Friday, 5 August 2011

String Comparison in .NET

Hate stuff like this:

if (myValue == "true")

Like this:

int compareResult = string.Compare(myValue, true.ToString(), StringComparison.OrdinalIgnoreCase);

Two things.

  1. Explicit control over casing, nothing is assumed.
  2. No magic hard coded string i.e. true.ToString() versus “true”.

Note that true.ToString() is actually “True”.


About Me