C# String CompareTo() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

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

ParameterDescription
strBRequired. The string to compare with this instance.

Return Value

Return ValueDescription
Less than 0This string comes before the other.
0The strings compare as equal.
Greater than 0This 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");
        }
    }
}

How It Works

  1. Call on the instance. a.CompareTo(b) compares the caller to the argument—not a static String.CompareTo.
  2. Culture-sensitive default. Rules follow the current culture; for most Latin text, 'a' still comes before 'b'.
  3. Branch on the sign. result < 0 means the caller sorts before the argument.
  4. 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");
        }
    }
}

How It Works

  1. Null argument is fine. text.CompareTo(null) returns a positive value—any string is greater than null.
  2. Null caller is not. Instance methods need a live object; missing.CompareTo(...) throws.
  3. Guard the caller. Check for null before calling, or use static string.Compare which accepts two nullable strings.
  4. 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);
        }
    }
}

How It Works

  1. Start with an unsorted list. Four names in mixed order.
  2. Call Sort(). List<string>.Sort() compares pairs using each string’s CompareTo.
  3. Same int contract. Negative means “first is smaller,” so names end up A→Z.
  4. 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.

Next: Concat()

Learn how to join strings with String.Concat().

Concat() 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.

9 people found this page helpful