Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Strings and Text

C# String.Compare(), CompareTo() and Equals() methods

Reference for C# string.Compare(), CompareTo(), Equals() and ==: ordinal and culture comparison, ignoring case, sort order, and null handling.

C# has several ways to compare strings, and they answer two different questions. Equals and == ask "are these the same text?" and return a bool. string.Compare and CompareTo ask "which comes first?" and return a negative number, zero or a positive number.

The second choice is how to compare: by raw character values (ordinal) or by linguistic rules (culture). The default for == and Equals(string) is ordinal and case-sensitive. The default for Compare and CompareTo is culture-sensitive. Always state what you want with a StringComparison argument.

C#
bool same = string.Equals(a, b, StringComparison.OrdinalIgnoreCase);
int order = string.Compare(a, b, StringComparison.Ordinal);

Try it

C# Equality

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Ordering and sorting

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public static int Compare(string? strA, string? strB, StringComparison comparisonType)
public static int Compare(string? strA, string? strB, bool ignoreCase)
public static int Compare(string? strA, string? strB)
public static int Compare(string? strA, int indexA, string? strB, int indexB, int length, StringComparison comparisonType)
public static int CompareOrdinal(string? strA, string? strB)

public int CompareTo(string? strB)
public bool Equals(string? value)
public bool Equals(string? value, StringComparison comparisonType)
public static bool Equals(string? a, string? b)
public static bool Equals(string? a, string? b, StringComparison comparisonType)
public static bool operator ==(string? a, string? b)
Name Description
strA, strB The strings to compare. null is allowed.
comparisonType How to compare. See StringComparison.
ignoreCase Older form: true ignores case, using the current culture.
indexA, indexB, length Compare only a substring of each string.

Returns: Compare and CompareTo return less than zero if the first value comes before the second, zero if equal, greater than zero otherwise. The magnitude is meaningless: test < 0, == 0 and > 0, never == -1. Equals returns a bool.

Errors / undefined behavior: ArgumentException for an invalid StringComparison value, and ArgumentOutOfRangeException in the range overload when an index or length falls outside the strings. A null culture-sensitive comparison is never an error. Calling an instance method such as a.Equals(b) on a null reference throws NullReferenceException.

Usage notes

  • == is already a value comparison. Unlike many reference types, string overloads ==, so a == c is true when the characters match even for different objects. It is ordinal and case-sensitive.
  • Ordinal for machines, culture for people. Use Ordinal or OrdinalIgnoreCase for identifiers, paths, keys and protocol text. Use culture-aware comparison when sorting words that a person reads. In this site's runner, globalization is invariant, so culture results shown live are the invariant ones.
  • Null ordering. Compare(null, "a") is negative: null sorts first. Two nulls compare equal.
  • Do not use Compare for equality. Compare(a, b) == 0 works, but Equals expresses intent and is faster.
  • Hidden bug source: a.ToLower() == b.ToLower() allocates two strings and has the Turkish i problem. Use Equals(a, b, StringComparison.OrdinalIgnoreCase) instead.
  • CompareOrdinal is a shortcut for ordinal ordering and is the cheapest ordering call.
  • Sorting. Array.Sort and List<string>.Sort() with no comparer use the current culture. Pass StringComparer.Ordinal for stable, culture-independent order.
  • Embedded nulls and zero-width characters can make culture comparison report equal strings that look different in a debugger. Ordinal comparison never does.