C# String Equals() Method

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
String Methods

What You’ll Learn

The Equals() method checks whether two strings contain the same characters in the same order. It returns true or false—the foundation of password checks, name matching, configuration validation, and any logic that asks “are these two texts the same?”

01

Instance Method

a.Equals(b)

02

Static Method

string.Equals(a,b)

03

bool Return

true or false.

04

Case Sensitive

Default overload.

05

StringComparison

Ignore case option.

06

vs == Operator

When each fits.

Definition and Usage

In C#, Equals() compares two strings for content equality. You can call it as an instance method—first.Equals(second)—or as a static method—string.Equals(first, second)—which is especially useful when either value might be null.

Unlike Compare(), which returns a sort order as an integer, Equals() answers a simple yes/no question. Unlike Contains() or EndsWith(), it compares the entire string, not just a portion.

💡
Beginner Tip

For strings, a == b and a.Equals(b) compare the same character content. Prefer string.Equals(a, b) when a or b might be null—it returns true only when both are null, without throwing an exception.

📝 Syntax

Common overloads on System.String:

C#
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);

Parameters

  • value / a, b — the string(s) to compare. Static overloads handle null safely.
  • comparisonType — rules for comparison: e.g. Ordinal, OrdinalIgnoreCase, CurrentCultureIgnoreCase.

Return Value

true if both strings have identical character content under the chosen rules; false otherwise.

⚡ Quick Reference

GoalCode
Compare two stringsfirst.Equals(second)
Null-safe comparisonstring.Equals(a, b)
Ignore casestring.Equals(a, b, StringComparison.OrdinalIgnoreCase)
Simple equalitya == b (when neither is null)
Sort order neededstring.Compare(a, b)
Instance
a.Equals(b)

Basic check

Static
string.Equals(a, b)

Null-safe

Ignore case
Equals(a,b,OrdinalIgnoreCase)

Case-insensitive

Operator
a == b

Same content

Examples Gallery

Run with dotnet run. Each example shows a practical way to compare strings—from basic equality to case rules, null safety, and comparison with related methods.

📚 Getting Started

Compare two strings for exact content match.

Example 1 — Basic Equals() Usage

Compare pairs of strings and print whether they are equal.

C#
using System;

class Program {
    static void Main() {
        string firstString  = "Hello, C#!";
        string secondString = "Hello, C#!";
        string thirdString  = "Greetings!";

        bool equal12 = firstString.Equals(secondString);
        bool equal13 = firstString.Equals(thirdString);

        Console.WriteLine($"first == second? {equal12}");
        Console.WriteLine($"first == third?  {equal13}");
    }
}

How It Works

firstString and secondString contain identical characters, so Equals returns true. thirdString has different content, so the result is false. Reference identity does not matter—only character content counts.

Example 2 — Case Sensitivity

See how the default overload treats letter casing.

C#
using System;

class Program {
    static void Main() {
        string a = "C#";
        string b = "c#";

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

How It Works

Both Equals() and == use ordinal, case-sensitive comparison by default for strings. "C#" and "c#" are not equal because the letter C differs in case.

📈 Practical Patterns

Case-insensitive checks, null safety, and related APIs.

Example 3 — Case-Insensitive Equality

Use StringComparison.OrdinalIgnoreCase for login names, codes, or tags.

C#
using System;

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

        bool caseSensitive = input.Equals(expected);
        bool ignoreCase    = input.Equals(
            expected,
            StringComparison.OrdinalIgnoreCase);

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

How It Works

The two-argument overload ignores letter casing. Use OrdinalIgnoreCase for identifiers and protocol values; use CurrentCultureIgnoreCase when linguistic culture rules matter (such as user-facing words in a specific locale).

Example 4 — Null-Safe Static Equals()

Compare strings safely when either side might be null.

C#
using System;

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

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

        // a.Equals(c) would throw NullReferenceException here
    }
}

How It Works

Static string.Equals(a, b) handles null without throwing. Two null values are considered equal. Calling a.Equals(b) when a is null throws NullReferenceException—so use the static form for uncertain inputs.

Example 5 — Equals() vs Compare()

Equals answers “same?”; Compare answers “which comes first?”

C#
using System;

class Program {
    static void Main() {
        string x = "apple";
        string y = "banana";

        bool areEqual = string.Equals(x, y);
        int  order    = string.Compare(x, y, StringComparison.Ordinal);

        Console.WriteLine($"Equals(x, y):  {areEqual}");
        Console.WriteLine($"Compare(x, y):   {order}");
        Console.WriteLine($"(negative = x before y)");
    }
}

How It Works

Equals returns false because the strings differ. Compare returns -1 because "apple" comes before "banana" in ordinal order. Use Compare for sorting; use Equals for equality checks.

🚀 Common Use Cases

  • Password or token verification — compare user input to a stored value.
  • Configuration keys — match setting names or enum-like string values.
  • Command parsing — check if input equals "quit" or "exit".
  • Unit test assertions — verify expected vs actual string output.
  • Null-safe API validation — use static string.Equals on optional parameters.

🧠 How Equals() Works

1

You provide two strings

Instance: a.Equals(b). Static: string.Equals(a, b).

Input
2

Runtime checks lengths

Different lengths mean false immediately. Null handled per overload.

Validate
3

Characters compared in order

Using ordinal or culture rules from StringComparison.

Compare
=

bool returned

true if every character matches; false on first difference or length mismatch.

📝 Notes

  • Default Equals(string) is ordinal and case-sensitive.
  • For strings, == compares content the same way as instance Equals.
  • Calling Equals on a null instance throws NullReferenceException.
  • Static string.Equals(a, b) is the safe choice when either value may be null.
  • Equals compares the full string—not prefix/suffix only.
  • Use StringComparison overloads instead of ToLower() for case-insensitive equality.

⚡ Optimization

Equals() and == for strings are both highly optimized in the .NET runtime—there is no meaningful performance reason to pick one over the other for equality checks. Choose based on readability and null safety. Avoid calling ToLower() on both strings before comparing; the StringComparison overload avoids extra allocations.

Conclusion

The C# Equals() method is the standard way to test whether two strings represent the same text. Instance and static overloads, plus StringComparison, cover everyday equality checks and null-safe patterns.

Practice the examples above, pick the right overload for your comparison rules, then continue to Format() for building formatted strings.

💡 Best Practices

✅ Do

  • Use string.Equals(a, b) when null is possible
  • Pass StringComparison for explicit comparison rules
  • Use == for simple non-null string checks
  • Use Compare() when you need sort order
  • Compare user input with the right case rules for your app

❌ Don’t

  • Call instance Equals on a possibly null reference
  • Use ToLower() on both strings when an overload exists
  • Confuse Equals with reference equality for objects
  • Use Equals when you only need prefix/suffix checks
  • Assume default Equals ignores case

Key Takeaways

Knowledge Unlocked

Five things to remember about Equals()

Use these points whenever you compare strings in C#.

5
Core concepts
02

bool Return

true or false.

Return
🛠 03

Static Form

Null-safe.

API
🔄 04

StringComparison

Case & culture.

Overload
📊 05

vs Compare()

Equality vs order.

Related

❓ Frequently Asked Questions

Equals() checks whether two strings have the same character content. The instance form is first.Equals(second); the static form string.Equals(a, b) compares two strings without needing an instance. Both return true when content matches and false when it does not.
text.Equals(other) or string.Equals(a, b). Add StringComparison for case/culture rules: text.Equals(other, StringComparison.OrdinalIgnoreCase) or string.Equals(a, b, StringComparison.OrdinalIgnoreCase).
It returns bool: true when both strings contain the same characters in the same order, false otherwise. It compares the full string—not just a prefix or suffix.
For string types, == compares character content the same way as Equals(). The difference is mostly style and null handling: calling Equals on a null reference throws, while string.Equals(a, b) safely returns true when both are null.
The default Equals(string) overload uses ordinal, case-sensitive comparison. For case-insensitive equality, use Equals(value, StringComparison.OrdinalIgnoreCase) or StringComparison.CurrentCultureIgnoreCase depending on your needs.
Use Equals() when you only need to know if two strings are the same (true/false). Use Compare() or CompareTo() when you need ordering—sorting, greater-than/less-than, or three-way comparison results.
Did you know?

For the string type specifically, the == operator compares character content, not object references. That is different from most other reference types, where == checks references unless overloaded. Equals() and == align for strings, but static string.Equals still wins when null values are possible.

Keep Building C# String Skills

Master string equality with Equals(), then continue to Format() for placeholder-based text.

Next: Format() →

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.

6 people found this page helpful