In C#, CompareTo() compares this string with another string and returns a signed int. It is the instance form of a culture-sensitive compare.
You pass the other string. It is required. The result is less than 0, 0, or greater than 0. Calling it on null throws.
Syntax
Call CompareTo on the first string. Start with this line.
C#
int r = a.CompareTo(b);
Call
What it does
a.CompareTo(b)
Compares a with b and returns a signed int.
There is no StringComparison overload on CompareTo. Prefer static Compare when you need clear comparison rules.
Library declarations
C#
public int CompareTo(string strB);
public int CompareTo(object value);
Parameters
Parameter
Description
strB
Required. The string to compare with this instance.
Return Value
Return Value
Description
Less than 0
This string comes before the other.
0
The strings compare as equal.
Greater than 0
This string comes after the other.
C# Examples
Example 1 — Basic Order with CompareTo()
Call CompareTo on "apple" and branch on the sign of the result.
C#
using System;
class Program {
static void Main() {
string a = "apple";
string b = "banana";
int result = a.CompareTo(b);
Console.WriteLine($"CompareTo 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
CompareTo result: -1
"apple" comes before "banana"
How It Works
Call on the instance.a.CompareTo(b) compares the caller to the argument—not a static String.CompareTo.
Culture-sensitive default. Rules follow the current culture; for most Latin text, 'a' still comes before 'b'.
Branch on the sign.result < 0 means the caller sorts before the argument.
Ignore exact magnitude. Rely on < 0 / == 0 / > 0—not a guaranteed -1 or 1.
Example 2 — Null Argument vs Null Caller
A null argument is allowed and sorts as less than any string. Calling on a null reference throws.
C#
using System;
class Program {
static void Main() {
string text = "C#";
int vsNull = text.CompareTo(null);
Console.WriteLine($"\"C#\".CompareTo(null) > 0? {vsNull > 0}");
string missing = null;
try {
missing.CompareTo("C#");
} catch (NullReferenceException) {
Console.WriteLine("null.CompareTo(...) throws NullReferenceException");
}
}
}
Null argument is fine.text.CompareTo(null) returns a positive value—any string is greater than null.
Null caller is not. Instance methods need a live object; missing.CompareTo(...) throws.
Guard the caller. Check for null before calling, or use static string.Compare which accepts two nullable strings.
Prefer clear equality APIs. For same-or-different checks, use == or Equals().
Example 3 — Sort with List<string>.Sort()
Default list sorting calls CompareTo() through IComparable<string>—no custom comparer required.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
var names = new List<string> { "Zoe", "Amy", "Bob", "Ada" };
names.Sort(); // uses string.CompareTo under the hood
foreach (string name in names) {
Console.WriteLine(name);
}
}
}
Output
Ada
Amy
Bob
Zoe
How It Works
Start with an unsorted list. Four names in mixed order.
Call Sort().List<string>.Sort() compares pairs using each string’s CompareTo.
Same int contract. Negative means “first is smaller,” so names end up A→Z.
Need custom rules? Pass a comparer, or use static string.Compare with StringComparison.
Frequently Asked Questions
CompareTo() compares this string with another and returns a signed int.
strB is required. It is the string to compare with this instance.
Less than 0, 0, or greater than 0. Only the sign is reliable.
No. Use the static String.Compare overloads when you need StringComparison.
Calling CompareTo on null throws NullReferenceException.