c# List去重

1 list若是數據是值類型,好比list<int> 這種,添加linq以後就可使用list = list.Distinct().ToList();ide

2 若是是數據是引用類型,好比中間是一個類List<Class>,由於他的比較是對哈希code進行比較,因此沒有辦法直接進行比較。可是能夠定義一下這個類的比較this

public class C : IEquatable<C>
    {
       public int name;
       public C(int _name)
       {
           name = _name;
       }

       public bool Equals(C other)
       {
           return this.name == other.name;
       }
       public override int GetHashCode()
       {
           return name.GetHashCode();
       }  
    }

這樣就將他的比較轉換爲內部關鍵數據name的比較了。spa

3 直接寫循環進行去重,這種就不說了。比較簡單code

相關文章
相關標籤/搜索