C# Char CompareTo() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Char

Definition

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);
CallWhat 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

ParameterDescription
valueRequired. The character (or boxed char) to compare against.

Return Value

Return ValueDescription
< 0This char is less than value.
0Equal.
> 0This 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");
    }
}

How It Works

  1. Code units. 'A' has a smaller UTF-16 value than 'C'.
  2. Negative means earlier. The signed result tells you relative order.
  3. Readable branch. Checking n < 0 prints a clear message.
  4. 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);
    }
}

How It Works

  1. Same value. Both variables hold digit nine.
  2. Zero means equal. CompareTo reports identity for ordering purposes.
  3. Operator check. == also confirms equality for everyday tests.
  4. 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);
    }
}

How It Works

  1. Cast to int. Printing (int)char shows the code unit.
  2. ASCII digits. '0'–'9' rise in order, so '2' is less than '7'.
  3. Difference magnitude. The absolute value can vary by implementation; trust the sign.
  4. 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.

Next: ConvertFromUtf32()

Convert a Unicode code point to a string of one or two UTF-16 chars with System.Char.

ConvertFromUtf32() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful