TICS Coding Standard Viewer 
TIOBE Software Quality Framework
Print-friendly version
©TIOBE Software www.tiobe.com
 
C# Coding Standard
Search

12@101

Synopsis:Do not use ToUpper() or ToLower() for case insensitive string comparison


Description:

Do not write code like:

if (x.ToLower() == y.ToLower()) { } // Compare two strings case insensitive

or

Hashtable x = new Hashtable(); if (x.ContainsKey(y.ToUpper()) { } // Compare Hasttable key case insensive.

Using ToLower() or ToUpper() create a new string which is not required, just use:

if (String.Compare(x, y, true) == 0) { }

or

Hashtable x = new Hashtable(CaseInsensitiveCompare.DefaultInvariant); // Use case insensive IEqualityComparer if (x.ContainsKey(y)) { }

Note: for .NET 1.1 and older use Hashtable constructor with IHashCodeProvider and IComparer.