In C#, String.Compare() compares two strings and returns a signed int. Use it when you need ordering, not only equality.
You pass the two strings. Both are required for the common call. The default compare is culture-sensitive. The result is less than 0, 0, or greater than 0.
Syntax
Pass the two strings. Start with this call.
C#
int r = string.Compare(a, b);
Call
What it does
string.Compare(a, b)
Compares a and b with culture-sensitive rules.
This is a static method. Only the sign of the result is reliable. Do not treat the int as a boolean.
Library declarations
C#
public static int Compare(string strA, string strB);
public static int Compare(string strA, string strB, StringComparison comparisonType);
Parameters
Parameter
Description
strA
Required. The first string.
strB
Required. The second string.
comparisonType
Optional. Rules for case and culture, such as OrdinalIgnoreCase.
Return Value
Return Value
Description
Less than 0
The first string comes before the second.
0
The strings compare as equal under the chosen rules.
Greater than 0
The first string comes after the second.
C# Examples
Example 1 — Basic Order with Compare()
Compare "apple" and "banana", then branch on the sign of the result.
C#
using System;
class Program {
static void Main() {
int result = string.Compare("apple", "banana");
Console.WriteLine($"Compare 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
Compare result: -1
"apple" comes before "banana"
How It Works
Call Compare.string.Compare("apple", "banana") uses culture-sensitive, case-sensitive rules by default.
First difference wins. At index 0, 'a' is less than 'b', so the result is negative (often -1).
Branch on the sign.result < 0 means the first string sorts before the second.
Ignore the exact magnitude. Rely on < 0 / == 0 / > 0—not a specific number like -1.
Example 2 — Ignore Case with StringComparison
Default compare treats "Hello" and "hello" as different. Use OrdinalIgnoreCase when case should not matter.
C#
using System;
class Program {
static void Main() {
string a = "Hello";
string b = "hello";
int sensitive = string.Compare(a, b);
int ignore = string.Compare(a, b, StringComparison.OrdinalIgnoreCase);
Console.WriteLine($"Case-sensitive equal? {sensitive == 0}");
Console.WriteLine($"Ignore case equal? {ignore == 0}");
}
}
Output
Case-sensitive equal? False
Ignore case equal? True
How It Works
Default is case-sensitive.Compare(a, b) treats H and h as different → not equal (== 0 is false).
Pass StringComparison.OrdinalIgnoreCase folds case without culture-specific letter rules.
Check equality with == 0.ignore == 0 means the strings match under those rules.
When to use Ordinal. Prefer ordinal options for file names, keys, and protocol strings—fast and stable across machines.
Example 3 — Sort Names with Array.Sort
Sorting uses the same < 0 / 0 / > 0 contract as Compare(). Pass a comparison that ignores case for user-facing names.
C#
using System;
class Program {
static void Main() {
string[] names = { "Zoe", "amy", "Bob", "alice" };
Array.Sort(names, (x, y) =>
string.Compare(x, y, StringComparison.OrdinalIgnoreCase));
foreach (string name in names) {
Console.WriteLine(name);
}
}
}
Output
alice
amy
Bob
Zoe
How It Works
Start with an unsorted array. Mixed case names would sort oddly if case mattered.
Pass a comparer. The lambda calls string.Compare with OrdinalIgnoreCase for each pair.
Array.Sort uses the int contract. Negative means swap order so the “smaller” name comes first.
Print the result. Names appear in A→Z order regardless of original casing.
Frequently Asked Questions
Compare() compares two strings and returns a signed int that shows order.
strA and strB are required. comparisonType is optional for case and culture rules.
Less than 0, 0, or greater than 0. Only the sign is reliable.
The default culture-sensitive overload follows culture rules. Pass StringComparison when you need a clear rule.
For yes/no equality, Equals() or == is clearer. Use Compare() when you need ordering.