Skip to main content

How to Compare Two Strings with == and != in C#

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Checking whether two pieces of text are the same is one of the most common things a program does: is this the right answer, the same user, the same choice? In C#, == compares two strings character by character, and != is its opposite.

Syntax​

Syntax
bool <same> = <text1> == <text2>;
bool <different> = <text1> != <text2>;
PartWhat it does
<text1> == <text2>true when both strings have the same characters in the same order, or both are null
<text1> != <text2>true when they differ
Upper and lower caseCount as different characters: "pizza" and "Pizza" aren't equal

Microsoft's equality operators reference: "Two string operands are equal when both of them are null or both string instances are of the same length and have identical characters in each character position". And: "String equality comparisons are case-sensitive ordinal comparisons."

The Example​

The program compares the word pizza with a copy built at run time, with Pizza in another case, and with null. Then it compares it with Pizza again, this time ignoring case.

Prerequisites​

  • The .NET 10 SDK or newer, which runs a single .cs file with dotnet run. This guide uses .NET SDK 10.0.404.

Step 1: Write the Code​

Create a file named main.cs in an empty folder:

main.cs, which sets a to pizza, b to PIZZA turned to lowercase, c to Pizza and none to null, then prints a == b, a == c, a != c, none == null, a == none, and string.Equals of a and c with StringComparison.OrdinalIgnoreCase.

main.cs
string a = "pizza";
string b = "PIZZA".ToLowerInvariant();
string c = "Pizza";
string? none = null;

Console.WriteLine(a == b);
Console.WriteLine(a == c);
Console.WriteLine(a != c);
Console.WriteLine(none == null);
Console.WriteLine(a == none);
Console.WriteLine(string.Equals(a, c, StringComparison.OrdinalIgnoreCase));

b is built while the program runs, by lowering the case of PIZZA, so it's a separate string object with the same characters as a.

Step 2: Run It​

Terminal
dotnet run main.cs
Output
True
False
True
True
False
True

A zsh terminal where dotnet run main.cs prints True, False, True, True, False and True.

  1. a == b is True: two separate strings, same characters.
  2. a == c is False, and a != c is True: P and p are different characters.
  3. none == null is True, and a == none is False. Comparing with null never throws.
  4. string.Equals with StringComparison.OrdinalIgnoreCase ignores case, so pizza and Pizza are equal there.

The command is the same in zsh, bash and PowerShell.

When One Side Is an object​

== compares characters only when C# sees two strings. If one side is typed as object, it checks whether both sides are the very same object instead, and warns you:

main.cs
object a = "pizza";
string b = "PIZZA".ToLowerInvariant();

Console.WriteLine(a == b);
Console.WriteLine((string)a == b);
Output
/Users/you/demo/main.cs(4,19): warning CS0252: Possible unintended reference comparison; to get a value comparison, cast the left hand side to type 'string'
False
True

Same characters, but False, because a and b are different objects. Casting a to string, as the warning suggests, gave True.

Common Mistakes​

MistakeWhat happensDo this instead
Expecting "pizza" == "Pizza" to be trueFalse: the comparison is case-sensitivestring.Equals(a, c, StringComparison.OrdinalIgnoreCase)
Comparing a string held in an object variable with ==warning CS0252, and False for equal textCast it to string first
Ignoring warning CS0252It marks a comparison of objects, not of textFix it as the warning says

Troubleshooting​

SymptomLikely causeFix
== is False for text that looks the sameDifferent case, or a space more or lessCompare ignoring case, or trim the text first
warning CS0252: Possible unintended reference comparisonOne side of == is typed as objectCast that side to string

FAQ​

Can I compare a string with null using ==?​

Yes, and it never throws: none == null was True and a == none was False.

Should I use == or Equals for strings?​

For two strings, == compares their characters, and it's the shortest to read. Use string.Equals with a StringComparison when you need to choose how to compare, such as ignoring case.

Conclusion​

text1 == text2 is true when both strings have the same characters, case included, and != when they don't. null compares without errors, string.Equals(..., StringComparison.OrdinalIgnoreCase) ignores case, and a CS0252 warning means one side isn't seen as a string.

Additional Resources​