Friday, July 08, 2005 - 12:58

Strings in .Net 2.0

I still haven't got around to installing Beta2 - I've been too busy getting a new laptop screen, partitioning my drive, reinstalling windows, deciding I needed win98 as well, repartitioning, installing 98, reinstalling XP, trying to get them to co-exist, deciding I don't need 98 after all, repartitioning, and reinstalling XP - but once I've reinstalled the dell-specific drivers I'll get right to it (can you believe, even the modem won't work with just the normal XP install?)

But it does look as though 2.0 has a lot of cool features, way more than just the couple of things that were mentioned at DevDays (although those were more than enough to make me happy). I've recently been doing some complicated forms-based stuff, and partial classes would really have been good there; but what I found today (off the dotnet.org.za main feed) was the new string handling options.

For ages I've been trying to convince colleagues that things like ToUpper() are horribly slow and should be avoided. Now it's easier to do this. Microsoft have acknowledged that "Sometimes strings should be allowed to vary according to the user's culture (for display data), but for most strings internal to an application, such as XML tags, user names, file paths, and system objects, the interpretation should be consistent throughout all cultures", which makes perfect sense. So they've introduced a StringComparison parameter to most string methods, such as:
String.Compare(protocol, "ftp", StringComparsion.Ordinal)
This not only makes it a lot faster, since it works on a byte comparison, but prevents the internal behaviour of your app changing unpredictably based on the current culture. These ordinal comparisons can be case sensitive or insensitive, and, the coolest part, .Equals() and == have been defined to use them by default. On the other hand, String.Compare() uses current culture semantics by default, so I guess it's better to explicity use the StringComparison to make sure that it works the way you intend. Especially since it's not particularly consistent: String.ToUpper and Char.ToUpper are both current culture, but IndexOf(string) is current culture while IndexOf(char) is ordinal.

ToUpper() (and ToLower()) still use the current culture, and while you can use ToUpperInvariant(), it's recommended that you just use String.Compare with StringComparison.OrdinalIgnoreCase instead.

This also affects Collections: amongst other changes, sort and compare methods now take a StringComparison parameter as well. Read the article yourself for the full details :-)

Labels:

0 Comments:

Post a Comment

<< Home