C# StringComparison Enum

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

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.

C#
text.Equals("hello", StringComparison.OrdinalIgnoreCase);
CallWhat it does
text.Equals(other, StringComparison.Ordinal)Exact match. "Hi" and "hi" are different.
text.Equals(other, StringComparison.OrdinalIgnoreCase)Match that ignores letter case.
text.Contains(part, StringComparison.OrdinalIgnoreCase)Substring search that ignores case.

StringComparison lives in the System namespace, so add using System; at the top.

Members

MemberDescription
OrdinalCompares raw character codes. Case-sensitive, fast, and the same on every machine.
OrdinalIgnoreCaseOrdinal comparison that ignores letter case. Good for file names, keys, and tags.
CurrentCultureUses the language rules of the current computer. Case-sensitive.
CurrentCultureIgnoreCaseCurrent culture rules, ignoring case.
InvariantCultureUses fixed, culture-neutral language rules. Case-sensitive.
InvariantCultureIgnoreCaseInvariant 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}");
    }
}

How It Works

  1. Same letters, different case. H and h are different characters.
  2. Ordinal is exact. It checks every character code, so the result is False.
  3. 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));
    }
}

How It Works

  1. Default is case-sensitive. "fun" does not match "Fun".
  2. Add a comparison. OrdinalIgnoreCase treats both as the same word.
  3. 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)}");
    }
}

How It Works

  1. Ordinal compares character codes. Lowercase a has a higher code than A, so the sign is 1.
  2. Ignore case. After case is ignored, both words match and the sign is 0.
  3. 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.

Next: String Interpolation

Build strings with $"Hi {name}" templates.

Interpolation tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful