So, I thought of another thing I’d like in my wish list of features I’d love to see in C# 5.0 and beyond. I’d like a format specifier to put the upper or lower case converted format argument into a formatted string.
That is, let’s say you are building a distributed cache key that takes as part of the key the host name, but sometimes you may get the same host name in upper, lower, or mixed case on your distributed cache provider is case sensitive in its comparison and you have no control over that.
You could do something like:
1 : var key = string.Format("cache.{0}.heartbeat", hostName.ToLower());
2 : _cache[key] = heartbeatTimeStamp;
But this creates a temp string for the lowercase form of hostName that has to be garbage collected later. This is in addition to the string you had to construct for the final formatted string itself.
What I’d like to see would be a format specifier flag you could add to insert the string argument in upper case or lower case that doesn’t create an unnecessary temp string, perhaps it could behave like the :G (general) flag and string.Format could recognize a :U and :L for upper and lower case respectively, like:
1 : var key = string.Format("cache.{0:L}.heartbeat", hostName);
2 : _cache[key] = heartbeatTimeStamp;
Yes, it’s small and seemingly insignificant, but when accessing a distributed cache on something performance sensitive, it would be nice to not have to create an extra temporary string if I can avoid it, and it seems the copy of the string into the underlying string buffer could be done in such a way as to convert to upper or lower in-line without having to allocate extra memory.
Just a thought. Your thoughts?
Tweet Technorati Tags: C#,.NET,String,String Format,Format Specifiers,C# 5.0
Print | posted on Monday, October 11, 2010 2:08 PM | Filed Under [ My BlogC#Software.NETvNext ]