C# String Equals() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In C#, Equals() checks whether two strings have the same characters. It returns true or false.

You can call it on a string or use the static form. The other value is required for the usual call. Pass StringComparison when you need clear case rules.

Syntax

Call Equals on the string, or use the static form.

C#
a.Equals(b);
string.Equals(a, b, StringComparison.OrdinalIgnoreCase);
CallWhat it does
a.Equals(b)Returns true when a and b have the same characters.
string.Equals(a, b, StringComparison.OrdinalIgnoreCase)Compares a and b while ignoring letter case.

Instance Equals on a null caller throws. The static form can handle nulls safely.

Library declarations
C#
public bool Equals(string value);
public bool Equals(string value, StringComparison comparisonType);
public static bool Equals(string a, string b, StringComparison comparisonType);

Parameters

ParameterDescription
valueRequired. The string to compare with this instance.
comparisonTypeOptional. Rules for case and culture, such as OrdinalIgnoreCase.

Return Value

Return ValueDescription
trueThe strings are equal under the chosen rules.
falseThe strings are not equal.

C# Examples

Example 1 — Basic Equals() and ==

Compare two identical strings with the instance method and with the == operator.

C#
using System;

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

        Console.WriteLine($"Equals: {a.Equals(b)}");
        Console.WriteLine($"==:     {a == b}");
    }
}

How It Works

  1. Same content. Both variables hold "CodeToFun".
  2. Instance Equals. a.Equals(b) compares characters left to right.
  3. Operator ==. For string, == also compares content—not object identity.
  4. Both true. Either style works for everyday equality checks.

Example 2 — Case-Sensitive vs Ignore Case

Default equality treats "Admin" and "admin" as different. Pass OrdinalIgnoreCase when case should not matter.

C#
using System;

class Program {
    static void Main() {
        string user = "Admin";
        string input = "admin";

        bool sensitive = user.Equals(input);
        bool ignore = user.Equals(input, StringComparison.OrdinalIgnoreCase);

        Console.WriteLine($"Case-sensitive: {sensitive}");
        Console.WriteLine($"Ignore case:    {ignore}");
    }
}

How It Works

  1. Default is case-sensitive. 'A' ≠ 'a' → false.
  2. Pass StringComparison. OrdinalIgnoreCase folds case without culture-specific letter rules.
  3. Login-style match. Ignore-case returns true.
  4. When to use Ordinal. Prefer ordinal options for usernames, keys, and protocol tokens.

Example 3 — Null-Safe Static Equals

Instance Equals on a null reference throws. Static string.Equals handles nulls safely.

C#
using System;

class Program {
    static void Main() {
        string a = null;
        string b = null;
        string c = "hi";

        Console.WriteLine($"Both null:  {string.Equals(a, b)}");
        Console.WriteLine($"null vs hi: {string.Equals(a, c)}");

        try {
            a.Equals(c);
        } catch (NullReferenceException) {
            Console.WriteLine("null.Equals(...) throws NullReferenceException");
        }
    }
}

How It Works

  1. Static both null. string.Equals(null, null) is true.
  2. Static null vs text. One missing side → false.
  3. Instance on null throws. You cannot call a method on a null reference.
  4. Prefer static for nullable data. Safer for optional strings from APIs or user input.

Frequently Asked Questions

Equals() checks whether two strings have the same characters and returns true or false.
value is required. comparisonType is optional for case and culture rules.
true when the strings match under the chosen rules, false otherwise.
Yes by default. Pass StringComparison.OrdinalIgnoreCase to ignore case.
Use Equals() for yes/no equality. Use Compare() when you need ordering.

Next: Format()

Learn composite formatting with String.Format().

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