In C#, CompareTo compares the current char with another character. The result follows UTF-16 numeric order: smaller code unit first.
Use it when you need ordering (sort keys, range logic). For a simple yes/no equality test, prefer Equals or the == operator.
This is an instance member of System.Char. You call it on a char value with a dot, such as c.CompareTo(...). A char holds one 16-bit UTF-16 code unit.
Syntax
Call on a char value and pass the other character.
C#
char a = 'A';
char b = 'C';
int n = a.CompareTo(b);
Call
What it does
a.CompareTo(b)
Negative if a < b, 0 if equal, positive if a > b.
a.CompareTo('A')
Compares against a char literal.
Ordering is by code unit value, not by language dictionary rules.
Library declarations
C#
public int CompareTo(char value);
public int CompareTo(object value);
Parameters
Parameter
Description
value
Required. The character (or boxed char) to compare against.
Return Value
Return Value
Description
< 0
This char is less than value.
0
Equal.
> 0
This char is greater than value.
C# Examples
Example 1 — Order Two Letters
Compare 'A' with 'C' and print the sign of the result.
C#
using System;
class Program {
static void Main() {
char a = 'A';
char b = 'C';
int n = a.CompareTo(b);
Console.WriteLine("CompareTo: " + n);
Console.WriteLine(n < 0 ? "A comes before C" : "A does not come before C");
}
}
Output
CompareTo: -2
A comes before C
How It Works
Code units.'A' has a smaller UTF-16 value than 'C'.
Negative means earlier. The signed result tells you relative order.
Readable branch. Checking n < 0 prints a clear message.
Not culture sort. This is numeric code-unit order, not a dictionary collator.
Example 2 — Zero When Equal
Show that identical characters return zero.
C#
using System;
class Program {
static void Main() {
char x = '9';
char y = '9';
Console.WriteLine(x.CompareTo(y));
Console.WriteLine(x == y);
}
}
Output
0
True
How It Works
Same value. Both variables hold digit nine.
Zero means equal. CompareTo reports identity for ordering purposes.
Operator check.== also confirms equality for everyday tests.
Pick the right tool. Prefer == when you do not need a sort key.
Example 3 — Digits Sort by Code
Compare '2' and '7' to see numeric digit order.
C#
using System;
class Program {
static void Main() {
char two = '2';
char seven = '7';
int n = two.CompareTo(seven);
Console.WriteLine("2 vs 7: " + n);
Console.WriteLine("Codes: " + (int)two + ", " + (int)seven);
}
}
Output
2 vs 7: -5
Codes: 50, 55
How It Works
Cast to int. Printing (int)char shows the code unit.
ASCII digits.'0'–'9' rise in order, so '2' is less than '7'.
Difference magnitude. The absolute value can vary by implementation; trust the sign.
Useful for ranges. Ordering helps when you build simple digit filters.
Frequently Asked Questions
A signed int: negative if this char is less, zero if equal, positive if greater.
No. It compares UTF-16 code units, not language-specific dictionary order.
Use == or Equals for equality. Use CompareTo when you need ordering.
Yes, there is an object overload; prefer the char overload for clarity.
ASCII digits compare in numeric order because their code units increase from 0 to 9.