Official Guidance: New Recommendations for Strings in .NET 2.0 [Dave Fetterman]

The BCL Team has here published new official guidance for using Strings in .NET 2.0.  It is highly recommended that .NET developers use these new best practices and new Framework types when developing on the second major version of the Framework.

The full paper can be found here:

The following is a snippet of the paper:


The Microsoft .NET Framework enables developers to create software fully ready for localization and internationalization, through comprehensive machinery under the hood designed for, among other tasks, correctly interpreting strings given the current locale. This aids in quickly creating and using solutions designed for a broad range of cultures. But, when culturally-irrelevant string data is interpreted by these methods, code can exhibit subtle bugs and operate slower than necessary on untested cultures.

When interpreting strings, the canonical example of a culturally-aware type, sometimes flipping the culture switch causes unexpected results. The same strings can sort, case, and compare differently under different Thread.CurrentCulture settings. 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. Additionally, when strings represent such symbolic information, comparison operations should be interpreted entirely non-linguistically.

Recommendations for String Use

When developing with the 2.0 version of the .NET Framework, keeping a few very simple recommendations in mind will suffice to solve confusion about using strings.

  • DO: Use StringComparison.Ordinal or OrdinalIgnoreCase for comparisons as your safe default for culture-agnostic string matching.
  • DO: Use StringComparison.Ordinal and OrdinalIgnoreCase comparisons for increased speed.
  • DO: Use StringComparison.CurrentCulture-based string operations when displaying the output to the user.
  • DO: Switch current use of string operations based on the invariant culture to use the non-linguistic StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase when the comparison is linguistically irrelevant (symbolic, for example).
  • DO: Use ToUpperInvariant rather than ToLowerInvariant when normalizing strings for comparison.
  • DON'T: Use overloads for string operations that don't explicitly or implicitly specify the string comparison mechanism.
  • DON'T: Use StringComparison.InvariantCulture-based string operations in most cases; one of the few exceptions would be persisting linguistically meaningful but culturally-agnostic data.

Many new and recommended String method overloads consume a StringComparison parameter, making these choices explicit:

Example 1:

String protocol = MyGetUrlProtocol(); 

if (String.Compare(protocol, "ftp", StringComparsion.Ordinal) != 0)
throw new InvalidOperationException();

Example 2:

String filename = args[0];
StreamReader reader;

if (String.EndsWith(filename, "txt", StringComparison.OrdinalIgnoreCase))
reader = File.OpenText(filename);

Comments (14)

  1. Anonymous says:

    Dave Fetterman reported yesterday on the Official Guidance: New Recommendations for Strings in .NET…

  2. Anonymous says:

    Why ToUpperInvariant rather then ToLowerInvariant should be used for normalizing strings for comparison?

  3. michkap says:

    There is a small range of characters that do not roundtrip, snd going to lowercase will make these characters unavailable.

    For more info, see

  4. Anonymous says:

    An excellent paper, and an important one; one that deserves the widest possible readership. Before reading this article I hadn’t appreciated the extent to which ‘Ordinal’ obviates the use of ‘InvariantCulture’, at least in the realm of string comparisons. Another nice thing this article does is explain how OrdinalIgnoreCase works, something I wasn’t quite clear about before. The new StringComparison enumeration is a wonderful thing, and I strongly urge Microsoft to use it in as many places as possible – e.g. in the IndexOf, LastIndexOf, Contains and Replace methods of the String class (Ladybug suggestion FDBK26964).

  5. Anonymous says:

    Interesting finds this morning

  6. Anonymous says:

    Since these immortal words were spoken by the voice of Tim Blaney to Ally Sheedy, I think every…

  7. Anonymous says:

    Since these immortal words were spoken by the voice of Tim Blaney to Ally Sheedy, I think every…

  8. Anonymous says:

    Since these immortal words were spoken by the voice of Tim Blaney to Ally Sheedy, I think every…

  9. Hi, You may find this problem being arised by your customers. You can find the description in the KB:

  10. The Visual Studio 2010 and .NET Framework 4.0 CTP is available for download as of last week. The CTP

  11. Hírcsatorna says:

    A BCL csapat közzétette azokat az új képességeket, amiket a várhatóan ez év második felében megjelenő

Skip to main content