C# String CompareOrdinal() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In C#, String.CompareOrdinal() compares two strings by their numeric character codes. It is always case-sensitive and does not use culture alphabet rules.

You pass the two strings. Both are required. The result is less than 0, 0, or greater than 0. Use this when you want a fast, culture-free order.

Syntax

Pass the two strings. Start with this call.

C#
int r = string.CompareOrdinal(a, b);
CallWhat it does
string.CompareOrdinal(a, b)Compares a and b by character codes, case-sensitive.

This is a static method. It is not for linguistic sort order in a language. Only the sign of the result is reliable.

Library declarations
C#
public static int CompareOrdinal(string strA, string strB);

Parameters

ParameterDescription
strARequired. The first string.
strBRequired. The second string.

Return Value

Return ValueDescription
Less than 0The first string comes before the second by code points.
0The strings have the same characters.
Greater than 0The first string comes after the second by code points.

C# Examples

Example 1 — Basic Order with CompareOrdinal()

Compare "apple" and "banana" by code unit order, then branch on the sign.

C#
using System;

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

        Console.WriteLine($"CompareOrdinal 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 CompareOrdinal. string.CompareOrdinal("apple", "banana") compares UTF-16 code units—no culture table.
  2. First difference wins. At index 0, 'a' (97) is less than 'b' (98), so the result is negative.
  3. Branch on the sign. result < 0 means the first string sorts before the second.
  4. Ignore exact magnitude. Rely on < 0 / == 0 / > 0—ordinal results are often the code-unit difference, not only -1/1.

Example 2 — Case-Sensitive Ordinal Rules

"Hello" and "hello" differ by case. Ordinal comparison never ignores case on its own.

C#
using System;

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

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

        Console.WriteLine($"Ordinal equal?       {ordinal == 0}");
        Console.WriteLine($"Ignore-case equal?   {ignore == 0}");
        Console.WriteLine($"'H' code = {(int)'H'}, 'h' code = {(int)'h'}");
    }
}

How It Works

  1. Ordinal is case-sensitive. 'H' (72) ≠ 'h' (104), so CompareOrdinal returns non-zero.
  2. Need ignore-case? Use string.Compare(a, b, StringComparison.OrdinalIgnoreCase)—still ordinal, but case folded.
  3. Check with == 0. Print equality flags instead of assuming the exact int value.
  4. Why this matters. File systems and APIs often treat case as significant; ordinal matches that model.

Example 3 — Partial Compare Overload

Compare only a slice of each string—useful for prefixes without allocating substrings.

C#
using System;

class Program {
    static void Main() {
        string a = "file.txt";
        string b = "file.log";

        // Compare first 4 characters: "file" vs "file"
        int prefix = string.CompareOrdinal(a, 0, b, 0, 4);

        // Compare from index 5: "txt" vs "log"
        int ext = string.CompareOrdinal(a, 5, b, 5, 3);

        Console.WriteLine($"Prefix equal? {prefix == 0}");
        Console.WriteLine($"Ext equal?    {ext == 0}");

        if (ext > 0) {
            Console.WriteLine("\"txt\" comes after \"log\" ('t' > 'l')");
        }
    }
}

How It Works

  1. Pick start indexes and length. CompareOrdinal(a, 0, b, 0, 4) compares four characters from the start of each string.
  2. Prefix matches. Both start with "file" → prefix == 0.
  3. Compare extensions. From index 5, "txt" vs "log"; 't' > 'l' so ext > 0.
  4. No Substring needed. The overload avoids allocating new strings when you only need a segment compare.

Frequently Asked Questions

CompareOrdinal() compares two strings by numeric character codes and returns a signed int.
Both are required. strA is the first string, and strB is the second.
Less than 0, 0, or greater than 0. Only the sign is reliable.
Yes. Always.
Use it for culture-free, case-sensitive ordering by character codes.

Next: CompareTo()

Learn instance IComparable string comparison.

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