When == is used on an expression of type object, it'll resolve to System.Object.ReferenceEquals.

Equals is just a virtual method and behaves as such, so the overridden version will be used (which, for string type compares the contents).

Answer from Mehrdad Afshari on Stack Overflow
Top answer
1 of 16
544

When == is used on an expression of type object, it'll resolve to System.Object.ReferenceEquals.

Equals is just a virtual method and behaves as such, so the overridden version will be used (which, for string type compares the contents).

2 of 16
459

When comparing an object reference to a string (even if the object reference refers to a string), the special behavior of the == operator specific to the string class is ignored.

Normally (when not dealing with strings, that is), Equals compares values, while == compares object references. If two objects you are comparing are referring to the same exact instance of an object, then both will return true, but if one has the same content and came from a different source (is a separate instance with the same data), only Equals will return true. However, as noted in the comments, string is a special case because it overrides the == operator so that when dealing purely with string references (and not object references), only the values are compared even if they are separate instances. The following code illustrates the subtle differences in behaviors:

string s1 = "test";
string s2 = "test";
string s3 = "test1".Substring(0, 4);
object s4 = s3;  // Notice: set to object variable!

Console.WriteLine($"{object.ReferenceEquals(s1, s2)} {s1 == s2} {s1.Equals(s2)}");
Console.WriteLine($"{object.ReferenceEquals(s1, s3)} {s1 == s3} {s1.Equals(s3)}");
Console.WriteLine($"{object.ReferenceEquals(s1, s4)} {s1 == s4} {s1.Equals(s4)}");

The output is:

True True True     // s1, s2
False True True    // s1, s3
False False True   // s1, s4

Summary:

Variables .ReferenceEquals == .Equals
s1, s2 True True True
s1, s3 False True True
s1, s4 False False True
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί c language β€Ί what-is-the-difference-between-assignment-and-equal-to-operators
What is the difference between = (Assignment) and == (Equal to) operators - GeeksforGeeks
July 11, 2025 - // C program to demonstrate // working of Assignment operators #include <stdio.h> int main() { // Assigning value 10 to a // using "=" operator int a = 10; printf("Value of a is %d\n", a); return 0; } ... The β€˜==’ operator checks whether the two given operands are equal or not.
🌐
Tutorial Teacher
tutorialsteacher.com β€Ί articles β€Ί equality-operator-vs-equals-method-in-csharp
Difference between == and Equals() Method in C#
In C#, the equality operator == checks whether two operands are equal or not, and the Object.Equals() method checks whether the two object instances are equal or not.
🌐
C# Corner
c-sharpcorner.com β€Ί UploadFile β€Ί 3d39b4 β€Ί difference-between-operator-and-equals-method-in-C-Sharp
Difference Between Equality Operator ( ==) and Equals() Method in C#
June 7, 2023 - The Equality Operator ( ==) is the comparison operator and the Equals() method compares the contents of a string. The == Operator compares the reference identity while the Equals() method compares only contents. Let’s see with some examples.
🌐
Filo
askfilo.com β€Ί cbse β€Ί smart solutions β€Ί what is the difference between the = and == operators in c? pr
What is the difference between the = and == operators in C? Provide an ex..
November 17, 2024 - In C, the '=' operator is the assignment operator, which is used to assign a value to a variable. The '==' operator is the equality operator, which is used to compare two values to check if they are equal.
🌐
LinkedIn
linkedin.com β€Ί pulse β€Ί should-i-use-equals-instead-compare-strings-c-igor-lashchenko
Should I use Equals instead of == to compare strings in C#?
October 19, 2021 - Equals looks safer, but if string ... Basically, main difference between Equals and == is that Equals will throw the null reference exception in case left side is null....
🌐
Quora
quora.com β€Ί What-is-the-difference-between-equal-to-and-double-equal-in-C-programming
What is the difference between equal to and double equal in C programming? - Quora
It assigns value on the right hand side of the variable to that variable. Example a = 5; The value of a is 5 now == Is the equality operator. It checks for equality and returns true if equal and false if they're not equal.
🌐
GitConnected
levelup.gitconnected.com β€Ί difference-between-equals-method-and-operator-in-c-6be00207a837
Difference Between Equals() Method and == Operator in C# | by Onur Derman | Level Up Coding
May 14, 2023 - This is because the Equals() method checks for type compatibility before comparing the values, and in this case, x is an int and y is a byte. When calling y.Equals(x), the byte value y is implicitly converted to an int, which results in a different value being compared.
Find elsewhere
🌐
Medium
medium.com β€Ί @teshwithnaidu β€Ί what-is-the-difference-between-and-equals-in-c-2cedbea4a39d
What is the difference between == and .Equals() in C#? | by Teshwithnaidu | Medium
August 23, 2025 - Unlike ==, .Equals() cannot be overloaded as an operator but can be overridden in custom classes to define equality logic. ... Console.WriteLine(a == b); // True (string overrides ==) Console.WriteLine(a.Equals(b)); // True (string overrides ...
🌐
Linux Hint
linuxhint.com β€Ί difference-between-assignment-and-equal-operators-c
What is the Difference Between = and == Operators in C Programming? – Linux Hint
The = operator is utilized to assign a value to a variable, while the == operator compares two variables or constants.
🌐
Sololearn
sololearn.com β€Ί en β€Ί Discuss β€Ί 163136 β€Ί what-is-the-difference-between-and-
What is the difference between " = " and " == " ?? | Sololearn: Learn to code for FREE!
"=" is an assignment operator which ... ... single `=` means you are giving the value example a=2 and == means equal sign with this will returm is true or false example 1==1...
🌐
Quora
quora.com β€Ί What-is-the-different-between-in-C
What is the different between = & == in C? - Quora
Answer (1 of 13): = and == are operators you come across in many programming languages. = is the assignment operator whereas == is an operator used to compare two operands. An example could help you understand the difference. Consider an integer variable x. Now you use = operator if you may want...
🌐
Medium
medium.com β€Ί dot-net-sql-learning β€Ί do-you-know-the-difference-between-and-equals-in-c-86e64b333edf
Do you know the difference between == and .Equals() in C# | by Code Crack | Dot Net, API & SQL Learning | Medium
March 8, 2025 - βœ”οΈ In case of Reference Type (string, object, class etc.): When the == operator is applied to a reference type (e.g. string, object, class), it usually compares the reference (memory address of the object), unless the == operator is overridden. object obj1 = new object(); object obj2 = new object(); Console.WriteLine(obj1 == obj2); // Output: False Β· πŸ”Ή Here obj1 and obj2 are two different objects (different allocations), so the == operator will return false.
🌐
University of Utah
users.cs.utah.edu β€Ί ~germain β€Ί PPS β€Ί Topics β€Ί equality.html
The C Programming Language - Command Line Arguments
To test if two variables contain equal values, we use the == notation. Here is an example: x = input('Person 1, give me a number: '); y = input('Person 2, give me a number: '); if ( x == y ) fprintf('You both think alike!\n'); end
🌐
LabEx
labex.io β€Ί questions β€Ί what-is-the-difference-between-and-strcmp-136079
What is the difference between equals and strcmp in C Programming | LabEx
Comparison Semantics: The == operator checks for value equality, while the strcmp() function compares the lexicographical order of the strings. Here's a visual representation of the differences using a Mermaid diagram:
🌐
Microsoft Learn
learn.microsoft.com β€Ί en-us β€Ί cpp β€Ί cpp β€Ί equality-operators-equal-equal-and-exclpt-equal
Equality operators: == and != | Microsoft Learn
The not-equal-to operator (!=) returns true if the operands don't have the same value; otherwise false. In C and C++, not_eq can be used as alternative to !=. For more information, see not-eq.
🌐
Quora
quora.com β€Ί What-is-the-difference-between-β€œ-and-”-in-C++
What is the difference between β€œ= and ==” in C++? - Quora
Answer (1 of 165): Difference between Assignment (=) Vs Equal to (==) Operators in C++ Many times this question arises what is the difference between = and == operators in C programming language? Here we are going to tell you exactly what the differences between these two operators are. Assignm...