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.
Fundamentals
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.
Foundation
📝 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.
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.
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}");
}
}
📤 Output:
Default Equals: False
== operator: False
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.
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
}
}
📤 Output:
null vs null: True
null vs hello: False
hello vs null: False
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)");
}
}
📤 Output:
Equals(x, y): False
Compare(x, y): -1
(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.
Applications
🚀 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.
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.
Important
📝 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.
Performance
⚡ 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.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Equals()
Use these points whenever you compare strings in C#.
5
Core concepts
📑01
Content Match
Same characters?
Basics
✅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.