James Michael Hare

...hare-brained ideas from the realm of software development...
posts - 166 , comments - 1431 , trackbacks - 0

My Links


Welcome to my blog! I'm a Sr. Software Development Engineer in the Seattle area, who has been performing C++/C#/Java development for over 20 years, but have definitely learned that there is always more to learn!

All thoughts and opinions expressed in my blog and my comments are my own and do not represent the thoughts of my employer.

Blogs I Read

Follow BlkRabbitCoder on Twitter

Tag Cloud

Article Categories




Little Wonders

Little Wonders


C# 5.0 And Beyond: Upper/Lower Case Format Specifiers

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?


 Technorati Tags: ,,,,,


Print | posted on Monday, October 11, 2010 2:08 PM | Filed Under [ My Blog C# Software .NET vNext ]

Powered by: