In C#, StringComparison is an enum that tells a string method how to compare text. It decides two things: whether letter case matters, and whether culture (language) rules are used.
You pass it as the last argument to methods such as Equals(), Contains(), StartsWith(), and Compare(). If you leave it out, the method picks its own default, which can surprise you.
Syntax
Pass one of the enum members as the last argument.
StringComparison lives in the System namespace, so add using System; at the top.
Members
Member
Description
Ordinal
Compares raw character codes. Case-sensitive, fast, and the same on every machine.
OrdinalIgnoreCase
Ordinal comparison that ignores letter case. Good for file names, keys, and tags.
CurrentCulture
Uses the language rules of the current computer. Case-sensitive.
CurrentCultureIgnoreCase
Current culture rules, ignoring case.
InvariantCulture
Uses fixed, culture-neutral language rules. Case-sensitive.
InvariantCultureIgnoreCase
Invariant culture rules, ignoring case.
C# Examples
Example 1 — Equals() with Ordinal Options
Compare "Hello" and "hello" using two different options.
C#
using System;
class Program {
static void Main() {
string a = "Hello";
string b = "hello";
bool exact = a.Equals(b, StringComparison.Ordinal);
bool ignore = a.Equals(b, StringComparison.OrdinalIgnoreCase);
Console.WriteLine($"Ordinal: {exact}");
Console.WriteLine($"OrdinalIgnoreCase: {ignore}");
}
}
Output
Ordinal: False
OrdinalIgnoreCase: True
How It Works
Same letters, different case.H and h are different characters.
Ordinal is exact. It checks every character code, so the result is False.
OrdinalIgnoreCase folds case. The two strings now count as equal.
Example 2 — Contains() Ignoring Case
Search for a word without worrying about capital letters.
C#
using System;
class Program {
static void Main() {
string text = "C# Is Fun";
Console.WriteLine(text.Contains("fun"));
Console.WriteLine(text.Contains("fun", StringComparison.OrdinalIgnoreCase));
}
}
Output
False
True
How It Works
Default is case-sensitive."fun" does not match "Fun".
Add a comparison.OrdinalIgnoreCase treats both as the same word.
Same idea everywhere.StartsWith() and EndsWith() accept it too.
Example 3 — Compare() Sign with Ordinal Options
string.Compare() returns a number. Use Math.Sign() to read it as -1, 0, or 1.
C#
using System;
class Program {
static void Main() {
int ordinal = string.Compare("apple", "Apple", StringComparison.Ordinal);
int ignore = string.Compare("apple", "Apple", StringComparison.OrdinalIgnoreCase);
Console.WriteLine($"Ordinal sign: {Math.Sign(ordinal)}");
Console.WriteLine($"OrdinalIgnoreCase sign: {Math.Sign(ignore)}");
}
}
Output
Ordinal sign: 1
OrdinalIgnoreCase sign: 0
How It Works
Ordinal compares character codes. Lowercase a has a higher code than A, so the sign is 1.
Ignore case. After case is ignored, both words match and the sign is 0.
Read the sign. Negative means first is smaller, zero means equal, positive means first is larger.
Frequently Asked Questions
It is an enum that tells string methods whether to ignore case and whether to use culture rules when comparing text.
Use Ordinal or OrdinalIgnoreCase for keys, file names, and code text. Use culture options for text shown to users.
Ordinal compares raw character codes. InvariantCulture applies fixed language rules that treat some characters differently.
Equals, Contains, StartsWith, EndsWith, IndexOf, Compare, and several others accept it.
No. Contains() is case-sensitive unless you pass StringComparison.OrdinalIgnoreCase or another ignore-case option.