Comparing two class instances

I have a class like this

public class TestData { public string Name {get;set;} public string type {get;set;} public List<string> Members = new List<string>(); public void AddMembers(string[] members) { Members.AddRange(members); } } 

I want to know if it is possible to directly compare with instances of this class with each other and find out that they are exactly the same? What is the mechanism? I look gor something like if(testData1 == testData2) //Do Something And if not, how to do it?

+7
source share
7 answers

You should implement the IEquatable<T> interface in your class, which will allow you to define your equality logic. In fact, you should override the Equals method.

 public class TestData : IEquatable<TestData> { public string Name {get;set;} public string type {get;set;} public List<string> Members = new List<string>(); public void AddMembers(string[] members) { Members.AddRange(members); } // Overriding Equals member method, which will call the IEquatable implementation // if appropriate. public override bool Equals( Object obj ) { var other = obj as TestData; if( other == null ) return false; return Equals (other); } public override int GetHashcode() { // Provide own implementation } // This is the method that must be implemented to conform to the // IEquatable contract public bool Equals( TestData other ) { if( other == null ) { return false; } if( ReferenceEquals (this, other) ) { return true; } // You can also use a specific StringComparer instead of EqualityComparer<string> // Check out the specific implementations (StringComparer.CurrentCulture, ea). if( EqualityComparer<string>.Default.Compare (Name, other.Name) == false ) { return false; } ... // To compare the members array, you could perhaps use the // [SequenceEquals][2] method. But, be aware that [] {"a", "b"} will not // be considerd equal as [] {"b", "a"} return true; } } 
+18
source

There are three ways to map objects of some reference type T :

  • Using the object.Equals
  • With implementation of IEquatable<T>.Equals (only for types that implement IEquatable<T> )
  • With comparison operator ==

In addition, for each of these cases, there are two possibilities:

  • Static type of compared objects T (or some other base T )
  • Static type of compared objects

Rules you definitely need to know:

  • By default for Equals and operator== it is necessary to check the reference equality
  • Equals implementations will work correctly no matter what static type of compared objects
  • IEquatable<T>.Equals should always behave the same as object.Equals , but if the static type of objects is T , it will offer slightly better performance.

So what does all this mean in practice?

Generally, you should use Equals to check for equality (overriding object.Equals as needed) and implement IEquatable<T> to provide slightly better performance. In this case, object.Equals should be implemented in terms of IEquatable<T>.Equals .

For some specific types (for example, System.String ) it is also acceptable to use operator== , although you should be careful not to make "polymorphic comparisons." Equals methods, on the other hand, will work correctly even if you make such comparisons.

You can see an example of polymorphic comparison and why this might be a problem here .

Finally, never forget that if you override object.Equals , you must also override object.GetHashCode .

+5
source

One way to do this is to implement IEquatable<T>

 public class TestData : IEquatable<TestData> { public string Name {get;set;} public string type {get;set;} public List<string> Members = new List<string>(); public void AddMembers(string[] members) { Members.AddRange(members); } public bool Equals(TestData other) { if (this.Name != other.Name) return false; if (this.type != other.type) return false; // TODO: Compare Members and return false if not the same return true; } } if (testData1.Equals(testData2)) // classes are the same 

You can also just override the Equals (object) method (from System.Object), if you do, you must also override GetHashCode see here

+4
source

You can override the equals method and inside it manually compare objects

See also Equals () and Operator == overload guidelines.

+1
source

You will need to define the rules that make object A equal to object B, and then override the Equals operator for this type.

http://msdn.microsoft.com/en-us/library/ms173147(v=vs.80).aspx

+1
source

Implement IEquatable<T> interface . This defines a generic method that a type or class of value implements to create a type-specific method for determining equality of instances. Further information here:

http://msdn.microsoft.com/en-us/library/ms131187.aspx

+1
source

First of all, equality is hard to define, and only you can determine what equality means to you.

  • Does this mean that members have the same meaning
  • Or they point to the same location.

Here is a discussion and answer here

What is Best Practice for comparing two instances of a reference type?

+1
source

All Articles