[英]Comparing objects from two instances of a Class and storing result in List<class>
[英]Comparing two instances of a class
我有這樣的課
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);
}
}
我想知道是否可以直接將此類的實例與彼此進行比較,並找出它們完全相同嗎? 機制是什么? 我正在尋找類似if(testData1 == testData2) //Do Something
做點什么,如果沒有,怎么做?
您應該在您的類上實現IEquatable<T>
接口,這將允許您定義相等邏輯。 實際上,您也應該重寫Equals
方法。
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, e.a.).
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;
}
}
可以將三種引用類型為T
對象彼此進行比較:
object.Equals
方法 IEquatable<T>.Equals
(僅適用於實現IEquatable<T>
類型) ==
此外,每種情況都有兩種可能性:
T
(或某些其它基礎T
) object
的靜態類型是object
您絕對需要知道的規則是:
Equals
和operator==
的默認值是測試引用是否相等 Equals
實現都能正常工作 IEquatable<T>.Equals
總是執行相同object.Equals
,但如果靜態類型的對象是T
,它會提供表現略好 那么,所有這些在實踐中意味着什么?
根據經驗,應該使用Equals
來檢查是否相等(覆蓋object.Equals
。必要時Equals
),並實現IEquatable<T>
來提供更好的性能。 在這種情況下object.Equals
應在以下方面來實現IEquatable<T>.Equals
。
對於某些特定類型(例如System.String
),也可以使用operator==
,盡管您必須注意不要進行“多態比較”。 另一方面,即使您進行了比較, Equals
方法也將正常工作。
您可以在此處看到一個多態比較的示例以及它為什么會成為問題的原因。
最后,不要忘記,如果重寫object.Equals
你還必須重寫object.GetHashCode
相應。
一種實現方法是實現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
您還可以重寫Equals(object)方法(來自System.Object),如果執行此操作,則還應該重寫GetHashCode, 請參見此處
我在這里看到了許多不錯的答案,但以防萬一您希望進行比較
if(testData1 == testData2) // DoSomething
可以使用==和!=運算符代替使用Equals函數:
public static bool operator == (TestData left, TestData right)
{
bool comparison = true; //Make the desired comparison
return comparison;
}
public static bool operator != (TestData left, TestData right)
{
return !(left == right);
}
您可以覆蓋equals方法,並在其中手動比較對象
您將需要定義使對象A等於對象B的規則,然后重寫此類型的Equals運算符。
http://msdn.microsoft.com/zh-CN/library/ms173147(v=vs.80).aspx
實現IEquatable<T> interface
。 這定義了一種通用方法,值類型或類實現該通用方法以創建用於確定實例相等性的特定於類型的方法。 此處有更多信息:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.