James Michael Hare

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

My Links

News

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

Archives

Post Categories

Image Galleries

.NET

CSharp

Little Wonders

Little Wonders

vNext

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 ]

Feedback

Gravatar

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

If you are really concerned about performance, you shouldn't use string.Format. I compared your code to the following code:
var key = "cache." + hostName.ToLower() + ".heartbeat";
and it was a little over twice as fast. I only use string.Format when I've got a lot of complex formatting or I don't care about performance.

Unless your hostName is very large then the built in string concatination is also faster than StringBuilder. I would probably take the hit from using ToLower or use a case insensitve Dictionary instead. Notice that the built in generic Dictionary has a constructor that takes an IEqualityComparer<TKey>. You can do the following to create a case insensitve dictionary:
new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase)
10/14/2010 9:04 PM | Jeff LeBert
Gravatar

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

@Jeff:

Good point on the case insensative comparator, but my point there was that some times that choice is outside of your control (like in a distributed cache such as Coherence).

I do agree that string.Format is better for complex formatting, as it is somewhat slower and that string concatenation is, on the whole, faster.

I've got an old post where I compare string concatenation vs string builder vs string.Format and string concatenation is faster for concatenations mostly (if you are doing multi-statement concatenations is is much slower and even for single statement concatenations if you use string builder and pre-allocate your buffer to the right size or greater then string builder is as fast -- which is roughly what C# does behind the scenes for concatenation, but of course that looks much uglier).

That said, I do still believe there is value in having an upper and lower case format specifier flag for those complex formatting strings where you do want to insert a string in upper or lower case and when you don't want to throw a lot of temporary strings on the GC.

I'm not saying never use ToUpper()/ToLower() or that we should favor string.Format() with an upper or lower flag to ToUpper()/ToLower() for all cases, just that it would be another nice tool in the wonderful .NET toolbox.

10/14/2010 11:30 PM | James Michael Hare
Gravatar

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

I'm wondering if anyone on this website would help me find out how to convert lower caps input into upper caps output(the way to code)
10/19/2011 4:32 AM | Katie Mooles
Gravatar

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

@Katie: Are you just talking about converting a lower-case input into an upper-case string? You can just call ToUpper() on the string. The main thing to realize is strings are immutable, thus ToUpper() doesn't convert the string itself, but returns a new string that is uppercase:

var text = "This is some text";

// upperText now has "THIS IS SOME TEXT" but original text unchanged.
var upperText = text.ToUpper();
10/19/2011 11:34 PM | James Michael Hare
Gravatar

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

@ Jeff Lebert ... you may find http://geekswithblogs.net/BlackRabbitCoder/archive/2010/05/10/c-string-compares-and-concatenations.aspx worth your time.

@ James Michael Hare

fantastic idea ... i hope the powrers that be hear you ... if implemented, your suggestion conforms imho to your advice "Code for readability, choose the best tool for the job which will usually be the most readable and maintainable as well."

regards, gerry (lowry)
12/19/2011 7:31 AM | gerry lowry
Gravatar

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

@Gerry: Thanks!
12/19/2011 4:26 PM | James Michael Hare
Gravatar

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

the one place I can think of where I would have liked this though is in Silverlight.
If you could use StringFormat property, you wouldn't need a custom IValueConverter
4/26/2013 7:20 PM | none
Gravatar

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

Another important argument for this feature is checking for null. If your string is a null reference, ToLower() and ToUpper() will cause a null reference exception. To avoid this, today you would have to write something like this:

string s1 = ????
string s2 = string.Format("{0}", (s1 != null) ? s1.ToUpper() : null);

With a format specifier you could write something like this:

string s2 = string.Format("{0:U}", s1);
11/10/2015 6:41 AM | Helge Kalnes
Post A Comment
Title:
Name:
Email:
Comment:
Verification:
 

Powered by: