For years now, Microsoft Developer Network (MSDN) has provided free online documentation to .NET developers. There is a lot of individual .NET best practices topics, which are described at the high level at this MSDN link:
MSDN: .NET Framework Best Practices
This is a great MSDN article to read and link to bookmark if you’re interested in.NET best practices.
Best Practices for Strings
Just take a look at all the information within the MSDN topic of Best Practices for Using Strings in the .NET Framework. I am not going to be able to duplicate all of that. If you are developing an application that has to deal with culture, globalization, and localization issues then you need to know much of this material.
Before I go any further, let me introduce you to Jon Skeet. He wrote an awesome book, C# In Depth. I think you might enjoy reading his online article on .NET Strings: http://csharpindepth.com/Articles/General/strings.aspx
Okay, let’s get back to the MSDN article. Below I have highlighted a few of the Strings best practices that I’d like to discuss.
1. Use the String.ToUpperInvariant method instead of the String.ToLowerInvariant method when you normalize strings for comparison.
In the .NET Framework, ToUpperInvariant is the standard way to normalize case. In fact, the Visual Studio Code Analysis has rule CA1308 in the Globalization category that can monitor this.
This is a really easy practice to follow once you know it.
Here is the key point I picked up from rule CA1308:
It is safe to suppress [this] warning message [CA1308] when you are not making security decision based on the result (for example, when you are displaying it in the UI).
In other words, take care to uppercase strings when the code is making a security decision based on normalized string comparison.
2. Use an overload of the String.Equals method to test whether two strings are equal.
Some of these overloads require a parameter that specifies the culture, case, and sort rules that are to be used in the comparison method. This just makes the string comparison you are using explicit.
3. Do not use an overload of the String.Compare or CompareTo method and test for a return value of zero to determine whether two strings are equal.
In the MSDN documentation for comparing Strings the guidance is quite clear:
The Compare method is primarily intended for use when ordering or sorting strings.
All-In-One Code Framework
If you have not had a chance to take a look at the All-In-One Code Framework then please take a few minutes to look it over.
The Microsoft All-In-One Code Framework is a free, centralized code sample library driven by developers’ needs.
It is Microsoft Public License (Ms-PL), which is the least restrictive of the Microsoft open source licenses.
What’s relevant to this article is the All-In-One Code Framework Coding Standards document. You can find the download link at the top of this page: http://1code.codeplex.com/documentation
In that document, they list a very relevant and useful list of String best practices.
- Do not use the ‘+’ operator (or ‘&’ in VB.NET) to concatenate many strings. Instead, you should use StringBuilder for concatenation. However, do use the ‘+’ operator (or ‘&’ in VB.NET) to concatenate small numbers of strings.
- Do use overloads that explicitly specify the string comparison rules for string operations. Typically, this involves calling a method overload that has a parameter of type StringComparison.
- Do use StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase for comparisons as your safe default for culture-agnostic string matching, and for better performance.
- Do use string operations that are based on StringComparison.CurrentCulture when you display output to the user.
- Do use the non-linguistic StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase values instead of string operations based on CultureInfo.InvariantCulture when the comparison is linguistically irrelevant (symbolic, for example). Do not use string operations based on StringComparison.InvariantCulture in most cases. One of the few exceptions is when you are persisting linguistically meaningful but culturally agnostic data.
- Do use an overload of the String.Equals method to test whether two strings are equal.
- Do not use an overload of the String.Compare or CompareTo method and test for a return value of zero to determine whether two strings are equal. They are used to sort strings, not to check for equality.
- Do use the String.ToUpperInvariant method instead of the String.ToLowerInvariant method when you normalize strings for comparison.
This post is part of my Compendium .NET Best-Practices series.