Tuesday, July 22, 2008

Some stuff that you did not know about Strings in .NET

  1. Did you know that all literal strings (string values known at compile time)  that have the same values will point at the same location.
    1. The reason for this is a .NET optimization called Intern Tables
  2. All strings created at run-time will be stored at separate memory addresses.
  3. Strings created at run-time can be interned too.
    1. var s1 = String.Format("{0} {1}", "Hello", "World");
    2. var s2 = String.Format("{0} {1}", "Hello", "World"); //s1 and s2 are stored at separate memory addresses
    3. var internedS1 = String.Intern( s1 ); 
    4. var internedS2 = String.Intern( s2 ); //internedS1 and internedS2 point to the same memory
  4. You can determine if a string is interned using the IsInterned method.


String Comparison

Different ways available to compare strings:

1. The String class’s instance Equals method

Calling convention: sLHS.Equals( sRHS)

Checks the values of the strings

Does not check for reference equality

2. The String class’s static Equals method

Calling convention: String.Equals(sLHS, sRHS);

First checks for reference equality and then performs the same checks as the instance Equals method.

Potentially faster than option 1, especially if many interned strings are present

3. The equality operator (==)

Calling convention: sLHS == sRHS

Uses the String class's static Equals method

Faster than 1 but slower than 2, as an extra call has to be made from the == method

4. The String class’s instance CompareTo method

Calling convention: sLHS.CompareTo(sRHS)

This is slow as it performs a much more indepth comparison of the the strings, taking into account information about culture, etc.

5. The String class’s static Compare method

Calling convention: String.Compare(sLHS,sRHS)

This too is much slower than options 1 to 4 as it uses a much more in-depth checking mechanism similar to the instance CompareTo method. Also provides useful overloads that allow you to perform comparisons that ignore case, etc.

6. .NET’s Regular Expression static RegEx.IsMatch method

This uses .NET's Regular Expression mechanism to compare and match strings. Certainly the slowest of all the available comparison methods.

Take away:

If performance is critical - then use either the instance or static Equals methods.If versatility is important and you want comparisons to work on strings originating from different cultures than stick to CompareTo or the static Compare methods.

More in-depth information is available at Stringing along in .NET… « akutz’s blog

No comments: