List some different ways for equality check in .Net

Technology CommunityCategory: C#List some different ways for equality check in .Net
VietMX Staff asked 3 years ago
  • The ReferenceEquals() method – checks if two reference type variables(classes, not structs) are referred to the same memory adress.
  • The virtual Equals() method. (System.Object) – checks if two objects are equivalent.
  • The static Equals() method – is used to handle problems when there is a null value in the check.
  • The Equals method from IEquatable interface.
  • The comparison operator == – usually means the same as ReferenceEquals, it checks if two variables point to the same memory adress. The gotcha is that this operator can be overrided to perform other types of checks. In strings, for instance, it checks if two different instances are equivalent.