Difference Between == and Equals() in C#

πŸ’‘ Concept: == vs Equals() in C#

Both == and Equals() are used to compare objects in C#, but they behave differently based on data type and context.

πŸ“˜ Quick Intro

The == operator checks for reference equality by default, while Equals() checks for value equality. However, value types override ==, and classes can override Equals().

🧠 Analogy

Imagine two books with identical content. Using == checks if they're the same physical copy (same memory address). Equals() checks if the content of both books is the same, regardless of which copy it is.

πŸ”§ Technical Explanation

  • == compares references for objects unless overloaded.
  • Equals() compares values/content by default.
  • For strings, both == and Equals() compare content.
  • Structs and primitive types override == to perform value comparison.
  • You can override Equals() to customize equality logic.

🎯 Use Cases

  • Use == for primitive types and strings when checking value equality.
  • Use Equals() when working with overridden equality logic or object types.
  • Use ReferenceEquals() to explicitly check object reference identity.

πŸ’» Real Code Example

string a = "hello";
string b = "hello";
Console.WriteLine(a == b);        // True
Console.WriteLine(a.Equals(b));   // True

object x = new object();
object y = new object();
Console.WriteLine(x == y);        // False
Console.WriteLine(x.Equals(y));   // False

int m = 5, n = 5;
Console.WriteLine(m == n);        // True
Console.WriteLine(m.Equals(n));   // True

❓ Interview Q&A

Q1: What is the main difference between == and Equals() in C#?

A: == compares references (for objects) while Equals() compares content.

Q2: How does == behave with strings?

A: It compares the string values, not references.

Q3: What happens if Equals() is not overridden?

A: It uses the default implementation from System.Object, which compares references.

Q4: Can structs override Equals()?

A: Yes, structs can override Equals() to define value-based comparison.

Q5: Is ReferenceEquals() same as ==?

A: No, ReferenceEquals() strictly checks if two references point to the same object.

Q6: How do collections behave with == and Equals()?

A: Collections use Equals() internally for searching and matching elements.

Q7: Can you override == in custom classes?

A: Yes, with operator overloading.

Q8: Should you override Equals() and GetHashCode() together?

A: Yes, to ensure consistent behavior in hash-based collections.

Q9: Which is safer to use in general: == or Equals()?

A: Equals(), especially for objects and custom types.

Q10: Is == a method in C#?

A: No, it is an operator and can be overloaded.

πŸ“ MCQs

Q1. What does == compare for objects by default?

  • Values
  • HashCode
  • References
  • Type

Q2. What does Equals() compare in objects?

  • References
  • Address
  • Content or value
  • Nullability

Q3. What is the result of 'hello' == 'hello'?

  • False
  • True
  • Error
  • Depends

Q4. Which one can be overridden in custom classes?

  • ==
  • Equals()
  • ReferenceEquals()
  • All

Q5. Which method checks if two references are the same?

  • Equals()
  • ==
  • Compare()
  • ReferenceEquals()

Q6. What does GetHashCode() relate to?

  • Sorting
  • Type casting
  • Overloading
  • Hash-based collections

Q7. What is true about == for value types?

  • Compares references
  • Compares types
  • Compares values
  • Throws exception

Q8. Which returns true for two identical strings?

  • Only Equals()
  • Only ==
  • Neither
  • Both == and Equals()

Q9. What class provides the base Equals() method?

  • System.ValueType
  • System.String
  • System.Base
  • System.Object

Q10. Is it necessary to override GetHashCode when Equals() is overridden?

  • No
  • Only for strings
  • Yes
  • Depends

πŸ’‘ Bonus Insight

Understanding == vs Equals() prevents subtle bugs in equality checksβ€”especially when working with custom classes or using collections like Dictionary or HashSet.

πŸ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

πŸ” Navigation

πŸ’¬ Feedback
πŸš€ Start Learning
Share:

Tags: