In C#, String.CompareOrdinal() compares two strings by their numeric character codes. It is always case-sensitive and does not use culture alphabet rules.
You pass the two strings. Both are required. The result is less than 0, 0, or greater than 0. Use this when you want a fast, culture-free order.
Syntax
Pass the two strings. Start with this call.
C#
int r = string.CompareOrdinal(a, b);
Call
What it does
string.CompareOrdinal(a, b)
Compares a and b by character codes, case-sensitive.
This is a static method. It is not for linguistic sort order in a language. Only the sign of the result is reliable.
Library declarations
C#
public static int CompareOrdinal(string strA, string strB);
Parameters
Parameter
Description
strA
Required. The first string.
strB
Required. The second string.
Return Value
Return Value
Description
Less than 0
The first string comes before the second by code points.
0
The strings have the same characters.
Greater than 0
The first string comes after the second by code points.
C# Examples
Example 1 — Basic Order with CompareOrdinal()
Compare "apple" and "banana" by code unit order, then branch on the sign.
C#
using System;
class Program {
static void Main() {
int result = string.CompareOrdinal("apple", "banana");
Console.WriteLine($"CompareOrdinal result: {result}");
if (result < 0) {
Console.WriteLine("\"apple\" comes before \"banana\"");
} else if (result > 0) {
Console.WriteLine("\"apple\" comes after \"banana\"");
} else {
Console.WriteLine("Strings are equal");
}
}
}
Output
CompareOrdinal result: -1
"apple" comes before "banana"
Ordinal is case-sensitive.'H' (72) ≠ 'h' (104), so CompareOrdinal returns non-zero.
Need ignore-case? Use string.Compare(a, b, StringComparison.OrdinalIgnoreCase)—still ordinal, but case folded.
Check with == 0. Print equality flags instead of assuming the exact int value.
Why this matters. File systems and APIs often treat case as significant; ordinal matches that model.
Example 3 — Partial Compare Overload
Compare only a slice of each string—useful for prefixes without allocating substrings.
C#
using System;
class Program {
static void Main() {
string a = "file.txt";
string b = "file.log";
// Compare first 4 characters: "file" vs "file"
int prefix = string.CompareOrdinal(a, 0, b, 0, 4);
// Compare from index 5: "txt" vs "log"
int ext = string.CompareOrdinal(a, 5, b, 5, 3);
Console.WriteLine($"Prefix equal? {prefix == 0}");
Console.WriteLine($"Ext equal? {ext == 0}");
if (ext > 0) {
Console.WriteLine("\"txt\" comes after \"log\" ('t' > 'l')");
}
}
}
Output
Prefix equal? True
Ext equal? False
"txt" comes after "log" ('t' > 'l')
How It Works
Pick start indexes and length.CompareOrdinal(a, 0, b, 0, 4) compares four characters from the start of each string.
Prefix matches. Both start with "file" → prefix == 0.
Compare extensions. From index 5, "txt" vs "log"; 't' > 'l' so ext > 0.
No Substring needed. The overload avoids allocating new strings when you only need a segment compare.
Frequently Asked Questions
CompareOrdinal() compares two strings by numeric character codes and returns a signed int.
Both are required. strA is the first string, and strB is the second.
Less than 0, 0, or greater than 0. Only the sign is reliable.
Yes. Always.
Use it for culture-free, case-sensitive ordering by character codes.