Wednesday, January 28, 2009


Here is a great article on when to use the StringComparison.

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.

Thursday, January 22, 2009

Use delegate to sort the generic list (collection)

Here is an example of how to use the delegate to sort the list:

returnList.Sort(delegate(IMyClass x, IMyClass y)
return String.Compare(x.Name, y.Name);

So, instead of override the ICompare, you can use this short-cut to return a sorted list.

Here is how to just make your Thing class implement IComparable, implementing the CompareTo method like this:

public int CompareTo(Thing other)
return Name.CompareTo(other.Name);