
StringComparer specifies how strings are compared. It is used as an argument to certain constructors and methods. It influences performance of collections such as Dictionary. Common values include Ordinal and OrdinalIgnoreCase.
This C# program uses StringComparer.Ordinal and StringComparer.OrdinalIgnoreCase.

First, this program demonstrates the StringComparer class when used as a reference to perform the Equals and Compare computations. Often, you will use StringComparer instances as a parameter to a collection such as a Dictionary or a sorting method, and this program does not show that.
However, it shows how you can acquire the reference to a StringComparer by accessing the StringComparer.Ordinal or StringComparer.OrdinalIgnoreCase property accessors in the System namespace. There are other comparers available as well.
Program that uses StringComparer instances [C#]
using System;
class Program
{
static void Main()
{
// Use these two StringComparer instances for demonstration.
StringComparer comparer1 = StringComparer.Ordinal;
StringComparer comparer2 = StringComparer.OrdinalIgnoreCase;
// First test the results of the Ordinal comparer.
Console.WriteLine(comparer1.Equals("value-1", "value-1")); // True
Console.WriteLine(comparer1.Equals("value-1", "VALUE-1")); // False
Console.WriteLine(comparer1.Compare("a", "b"));
Console.WriteLine(comparer1.Compare("a", "a"));
Console.WriteLine(comparer1.Compare("b", "a"));
// Test the results of the OrdinalIgnoreCase comparer.
Console.WriteLine(comparer2.Equals("value-1", "value-1")); // True
Console.WriteLine(comparer2.Equals("value-a", "value-b")); // False
Console.WriteLine(comparer2.Equals("value-1", "VALUE-1")); // True
Console.WriteLine(comparer2.Compare("a", "B"));
Console.WriteLine(comparer2.Compare("a", "A"));
Console.WriteLine(comparer2.Compare("b", "A"));
}
}
Output
True
False
-1
0
1
True
False
True
-1
0
1Description. The program gets references to the StringComparer class instances by accessing the properties with the composite names StringComparer.Ordinal and StringComparer.OrdinalIgnoreCase. The identifiers Ordinal and OrdinalIgnoreCase are simply names and do not determine how you can use the instances. You can pass the references to constructors such as the Dictionary constructor.
Ordinal and OrdinalIgnoreCase. Next this program shows what the result of using the StringComparer class on various input arguments. The Equals and Compare methods are instance methods on the StringComparer type. In this program, Ordinal and OrdinalIgnoreCase function exactly the same way, except with OrdinalIgnoreCase, lowercase and uppercase letters are treated as equal.
Note: You can see that the OrdinalIgnoreCase instance returns True for differently-cased parameter strings as evidence of this.

Let's address the need to provide the StringComparer reference type to certain collections such as the Dictionary collection, as well as the Array.Sort method overloads. You can simply pass the reference "StringComparer.Ordinal" (or others) to these methods and constructors. For a concrete example, please see the Dictionary optimization article on this subject.
Dictionary StringComparer Tip
We looked at the StringComparer type in the C# programming language, seeing a primitive example of its functionality and implementations of the Equals and Compare methods. The class also provides the GetHashCode method, which is useful for Dictionary and Hashtable types. Finally, you can get similar functionality through the StringComparison enumerated constant for certain string type methods.
StringComparison Enum String Type