Should I Overload == Operator?

How does the == operator work in C #? If it is used to compare objects of class A, will it try to match all properties of A or will it look for pointers to the same memory cell (or, possibly, something else)?

Let me create a hypothetical example. I am writing an application that uses the Twitter API and has a Tweet class that has all the properties of a single tweet: text, sender, date and time, source, etc. If I want to compare objects of the Tweet class for equivalence, can I just use:

 Tweet a, b; if (a == b) { //do something... } 

Will it check the equivalence of all properties of the Tweet class between a and b?

If not, would it be the right approach to overload the == operator to explicitly check the equivalence of all fields?

UPDATE:. Of the first two answers, I'm right in assuming:

  • If the == operator or the Equals method is not overloaded for the class, the == operator is used for the class of the object.
  • The == operator for an object class checks equality in a memory cell.
  • I need to overload the == operator or the Equals method to complete this task.
  • In overloading, I need to check the equivalence of properties manually, so there is no way to do this semi-automatically, say, in a loop , right?

UPDATE # 2: Yuri made a comment that you can check the equivalence in the properties of the == operator with reflection . How can this be done? Could you give me some sample code? Thanks!

+3
source share
5 answers

MSDN has a good example on how to do this:

  public override bool Equals(object o) { try { return (bool) (this == (DBBool) o); } catch { return false; } } 

Then you overload == and! =:

 // Equality operator. Returns dbNull if either operand is dbNull, // otherwise returns dbTrue or dbFalse: public static DBBool operator ==(DBBool x, DBBool y) { if (x.value == 0 || y.value == 0) return dbNull; return x.value == y.value? dbTrue: dbFalse; } // Inequality operator. Returns dbNull if either operand is // dbNull, otherwise returns dbTrue or dbFalse: public static DBBool operator !=(DBBool x, DBBool y) { if (x.value == 0 || y.value == 0) return dbNull; return x.value != y.value? dbTrue: dbFalse; } 

And don't forget to overload the GetHash method.

Edit:

I wrote the following quick sample to use reflection in comparison. This should be much more comprehensive, I could try to make a blog on it if people want me to:

 public class TestEquals { private int _x; public TestEquals(int x) { this._x = x; } public override bool Equals(object obj) { TestEquals te = (TestEquals)obj; if (te == null) return false; foreach (var field in typeof(TestEquals) .GetFields(BindingFlags.NonPublic | BindingFlags.Instance)) { if (!field.GetValue(this).Equals(field.GetValue(te))) return false; } return true; } } 
+3
source

For reference types, the default implementation of both the == operator and the Equals() method will simply verify that both objects have the same reference and therefore the same instance.

If you want to check the contents of two different objects, then you must write code to do it yourself, one way or another. It could be done with reflection ( MbUnit test environment does something in this direction), but with a large penalty for performance and a good chance that it will not do what you expected, and you must implement == or Equals and GetHashCode manually.

+4
source

The correct approach is to overload the equals method of the Tweet class in addition to the == operator, as described here .

+2
source

Will check the equivalence of all properties of the Tweet class between a and b?

Not

If not, will the correct approach overload the == operator to explicitly check the equivalence of all fields?

You can either overload the == operator or overload the Equals function.

Edit

@Yuriy gave a good example for comparing all non-public variables. Since I also wrote an example, here it is (my compares properties)

 class TwitterItem { private string myValue = "default value"; public string Value1 { get { return myValue; } set { myValue = value; } } public string Value2 { get { return myValue; } set { myValue = value; } } public string Value3 { get { return myValue; } set { myValue = value; } } public override bool Equals(object obj) { if (base.Equals(obj)) return true; Type type = typeof(TwitterItem); PropertyInfo[] properties = type.GetProperties(); foreach (PropertyInfo property in properties) { if (false == property.GetValue(this, null).Equals(property.GetValue(obj, null))) return false; } return true; } } 
+1
source

You can compare properties using reflection:

 var a = new Entity() { Name = "test", ID = "1" }; var b = new Entity() { Name = "test", ID = "1" }; var c = new Entity() { Name = "test", ID = "2" }; System.Diagnostics.Debug.WriteLine(a.Equals(b));//Returns true System.Diagnostics.Debug.WriteLine(a.Equals(c));//Returns false public class Entity { public string Name { get; set; } public string ID { get; set; } public override bool Equals(object obj) { var t = obj.GetType(); foreach (var p in t.GetProperties()) { if (t.GetProperty(p.Name).GetValue(obj, null) != t.GetProperty(p.Name).GetValue(this, null)) return false; } return true; } } 
+1
source

All Articles