Read the syntax
Compare identifiers with ordinal rules and distinguish them from culture-sensitive text intended for people.
The second explicitly selects case-insensitive ordinal equality and returns true.
What it is and why to use it
A protocol token or configuration key should not change meaning when the machine’s display language changes. StringComparison.Ordinal and OrdinalIgnoreCase make that intention explicit. Human-facing sorting and searching may need linguistic rules instead. The right comparison depends on what the text represents, not on a universal preference for ignoring case.
How it works
Pass a StringComparison value to supported methods such as string.Equals and StartsWith. Ordinal compares code units; OrdinalIgnoreCase uses ordinal case-insensitive rules rather than the current culture. StringComparer supplies comparable rules for collections. These choices do not perform Unicode normalisation, and not every domain, such as filesystem paths, has the same case-sensitivity requirements.
Common mistakes and limitations
Do not call ToLower merely to compare identifiers or assume every identifier is case-insensitive. Do not use ordinal ordering as a replacement for a language-aware address book sort. Decide a domain’s rules and apply the same equality policy to dictionaries and lookups.
Run the examples locally
These complete Program.cs examples use a .NET 10 console project with the C# language version set to 12.0, nullable checking enabled and implicit usings disabled. They use standard .NET libraries and need no extra NuGet packages. This is the verified environment, not a claim that every API requires .NET 10 or works unchanged in .NET Framework, Unity or other runtimes.
Install a supported .NET 10 SDK, create the console project, replace its project file with the settings below, then replace Program.cs with one complete example at a time. Build and run from the project folder. using directives bring namespaces into scope; top-level statements form the program’s entry point, and any helper types follow those statements. Console.WriteLine writes terminal output. Do not combine examples that declare the same type names.
C# in Codectionary’s Playground is editing-only: it does not compile or execute these programs. Run them with the local .NET SDK to reproduce the expected output. Async examples use controlled in-process operations rather than live network services or timing-sensitive delays.
Create and enter a project
dotnet new console -n CSharpExample --framework net10.0
cd CSharpExampleCSharpExample.csproj settings used for these examples
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<LangVersion>12.0</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>disable</ImplicitUsings>
</PropertyGroup>
</Project>Build and run after saving one example in Program.cs
dotnet build
dotnet run --no-buildContinue learning
Change one input and predict the result before running it. Follow the related guides to connect this topic with the surrounding language features.
Syntax
string.Equals(left, right, StringComparison.OrdinalIgnoreCase)Examples
Compare a trusted command name
Make the intended case-insensitive command comparison visible at the call site.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
string command = "HELP";
Console.WriteLine(string.Equals(command, "help", StringComparison.Ordinal));
Console.WriteLine(string.Equals(command, "help", StringComparison.OrdinalIgnoreCase));
- The first call compares exact ordinal text, so uppercase and lowercase differ.
- The second explicitly selects case-insensitive ordinal equality and returns true.
- This is suitable for an application-defined command whose documented rules ignore case; it does not establish user permissions.
- Expected output: False True
- Specify the comparison rule at important text boundaries.
Use the same rule for a lookup collection
Avoid creating separate entries for case variants of a logical setting key.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
var settings = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
settings["Theme"] = "dark";
Console.WriteLine(settings["theme"]);
Console.WriteLine(settings.ContainsKey("THEME"));
- The comparer is selected when the dictionary is constructed and governs its key hashing and equality.
- The lookup with lowercase theme finds the existing Theme entry.
- ContainsKey uses that same policy. Consistency between comparison and storage prevents contradictory lookup behaviour.
- Expected output: dark True
- Specify the comparison rule at important text boundaries.
Best practices
- Specify the comparison rule at important text boundaries.
- Keep linguistic presentation rules separate from identifier equality.
Runtime & versions
.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
C# language reference