C# String Compare() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In C#, String.Compare() compares two strings and returns a signed int. Use it when you need ordering, not only equality.

You pass the two strings. Both are required for the common call. The default compare is culture-sensitive. The result is less than 0, 0, or greater than 0.

Syntax

Pass the two strings. Start with this call.

C#
int r = string.Compare(a, b);
CallWhat it does
string.Compare(a, b)Compares a and b with culture-sensitive rules.

This is a static method. Only the sign of the result is reliable. Do not treat the int as a boolean.

Library declarations
C#
public static int Compare(string strA, string strB);
public static int Compare(string strA, string strB, StringComparison comparisonType);

Parameters

ParameterDescription
strARequired. The first string.
strBRequired. The second string.
comparisonTypeOptional. Rules for case and culture, such as OrdinalIgnoreCase.

Return Value

Return ValueDescription
Less than 0The first string comes before the second.
0The strings compare as equal under the chosen rules.
Greater than 0The first string comes after the second.

C# Examples

Example 1 — Basic Order with Compare()

Compare "apple" and "banana", then branch on the sign of the result.

C#
using System;

class Program {
    static void Main() {
        int result = string.Compare("apple", "banana");

        Console.WriteLine($"Compare 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 Compare. string.Compare("apple", "banana") uses culture-sensitive, case-sensitive rules by default.
  2. First difference wins. At index 0, 'a' is less than 'b', so the result is negative (often -1).
  3. Branch on the sign. result < 0 means the first string sorts before the second.
  4. Ignore the exact magnitude. Rely on < 0 / == 0 / > 0—not a specific number like -1.

Example 2 — Ignore Case with StringComparison

Default compare treats "Hello" and "hello" as different. Use OrdinalIgnoreCase when case should not matter.

C#
using System;

class Program {
    static void Main() {
        string a = "Hello";
        string b = "hello";

        int sensitive = string.Compare(a, b);
        int ignore = string.Compare(a, b, StringComparison.OrdinalIgnoreCase);

        Console.WriteLine($"Case-sensitive equal? {sensitive == 0}");
        Console.WriteLine($"Ignore case equal?    {ignore == 0}");
    }
}

How It Works

  1. Default is case-sensitive. Compare(a, b) treats H and h as different → not equal (== 0 is false).
  2. Pass StringComparison. OrdinalIgnoreCase folds case without culture-specific letter rules.
  3. Check equality with == 0. ignore == 0 means the strings match under those rules.
  4. When to use Ordinal. Prefer ordinal options for file names, keys, and protocol strings—fast and stable across machines.

Example 3 — Sort Names with Array.Sort

Sorting uses the same < 0 / 0 / > 0 contract as Compare(). Pass a comparison that ignores case for user-facing names.

C#
using System;

class Program {
    static void Main() {
        string[] names = { "Zoe", "amy", "Bob", "alice" };

        Array.Sort(names, (x, y) =>
            string.Compare(x, y, StringComparison.OrdinalIgnoreCase));

        foreach (string name in names) {
            Console.WriteLine(name);
        }
    }
}

How It Works

  1. Start with an unsorted array. Mixed case names would sort oddly if case mattered.
  2. Pass a comparer. The lambda calls string.Compare with OrdinalIgnoreCase for each pair.
  3. Array.Sort uses the int contract. Negative means swap order so the “smaller” name comes first.
  4. Print the result. Names appear in A→Z order regardless of original casing.

Frequently Asked Questions

Compare() compares two strings and returns a signed int that shows order.
strA and strB are required. comparisonType is optional for case and culture rules.
Less than 0, 0, or greater than 0. Only the sign is reliable.
The default culture-sensitive overload follows culture rules. Pass StringComparison when you need a clear rule.
For yes/no equality, Equals() or == is clearer. Use Compare() when you need ordering.

Next: CompareOrdinal()

Learn culture-independent ordinal string ordering.

CompareOrdinal() 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