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.
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
Parameter
Description
value
Required. The string to compare with this instance.
comparisonType
Optional. Rules for case and culture, such as OrdinalIgnoreCase.
Return Value
Return Value
Description
true
The strings are equal under the chosen rules.
false
The 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}");
}
}
Output
Equals: True
==: True
How It Works
Same content. Both variables hold "CodeToFun".
Instance Equals.a.Equals(b) compares characters left to right.
Operator ==. For string, == also compares content—not object identity.
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}");
}
}
Output
Case-sensitive: False
Ignore case: True
How It Works
Default is case-sensitive.'A' ≠ 'a' → false.
Pass StringComparison.OrdinalIgnoreCase folds case without culture-specific letter rules.
Login-style match. Ignore-case returns true.
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");
}
}
}
Output
Both null: True
null vs hi: False
null.Equals(...) throws NullReferenceException
How It Works
Static both null.string.Equals(null, null) is true.
Static null vs text. One missing side → false.
Instance on null throws. You cannot call a method on a null reference.
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.