.net - Comparing 2 custom objects - C# -


I need to type a generic method in the base class which will accept as 2 object parameter and for their likeness Will compare Public Intent Baccalaureate {Public Bull AreEqual (Object O1, Object O2) {// Required to apply this}} Public class DataTypeOne: BaseData {Name of the public string; Public string address; } Public class datatype two: Besadata {public int costID; Public String CustName; }

Accepts 2 examples of AreEqual () method DataTypeOne or 2 examples of DataTypeTwo Accepts

My guess is that I need to use reflection? I can use LINQ if it is more readable / abbreviated.

Edit: Reason: I would like to apply this method to the base class because there are a large number of Gods working on classes derived due to project restrictions. By implementing it in the Aadhar class, I am trying to keep 1 little thing to worry about them.

(Suppose that you want all areas of two objects for equality To compare.)

Usually, you will not be bothered to use the reflection for it, you only field your own for this purpose IEquatable & lt; T & gt; The interface is present, and you want to override the Object.Equals () on the types of questions. For example:

  public class datatype two: bassadata, iquateable & lt; Datatize to & gt; {Public Int CustId; Public String CustName; Public override int GetHashCode () {return CustId ^ CustName.GetHashCode (); // or whatever) public override bool par (object objects) {return.Equals (as in other datatype two); } Public Bull Equals (Datatype Two Other) {Return (Other! = Null & amp; Other; CostID == This Cost & amp; Other. Costname == this.CustName); }}  

Also, consider whether your type is understood as a struct instead, or the value type is automatically field- Comparison of comparisons by comparison by-by-field.

Keep in mind that by overriding equal , you are basically receiving (it seems to me) that you are trying with your "Master Equal Method" scheme Get it, people who use DataTypeTwo will be able to test naturally to know something specific about their API for equality - they just similar Will use things like they will do with the other.

Edit: Thank you for jared to remind me about GetHashCode . You also want to make sure that any two objects are "same", whichever ish has returned the code, by ensuring that this HashTable will also want to override it to maintain normal looking behavior.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -