C# List<T> Contains<T>()的用法小結(jié)
泛型List<T>中有一個比較列表是否已包含對象的方法Contains<T>(),今天在網(wǎng)上搜了一個用法,記錄下來,備查。
要用此方法比較我們的自定義對象,首先要有一個比較器,
要注意的是,這里的比較器是實(shí)現(xiàn)IEqualityComparer<T>接口的,不要寫成IComparer<T>。
如下:
/// <summary>
/// 描 述:彈出模型對象列表比較器(根據(jù)ID比較)
/// </summary>
public class PopupComparer : IEqualityComparer<Model.PopupModel.PopupModel>
{
public static PopupComparer Default = new PopupComparer();
#region IEqualityComparer<PopupModel> 成員
public bool Equals(Model.PopupModel.PopupModel x, Model.PopupModel.PopupModel y)
{
return x.Id.Equals(y.Id);
}
public int GetHashCode(Model.PopupModel.PopupModel obj)
{
return obj.GetHashCode();
}
#endregion
}
然后我們就可以調(diào)用Contains<T>()方法了,如下:
List<PopupModel> list = new List<PopupModel>();
//model為要比較的對象
if(list.Contains<PopupModel>(model,PopupComparer.Default))
{
}
- 淺談C#中List<T>對象的深度拷貝問題
- C#中將DataTable轉(zhuǎn)化成List<T>的方法解析
- C#常見的幾種集合 ArrayList,Hashtable,List<T>,Dictionary<K,V> 遍歷方法對比
- C#將DataTable轉(zhuǎn)化為List<T>
- C#基礎(chǔ)教程之IComparable用法,實(shí)現(xiàn)List<T>.sort()排序
- 關(guān)于C#泛型列表List<T>的基本用法總結(jié)
- C# List<T>的用法小結(jié)
- C#中IList<T>與List<T>的區(qū)別深入解析
- C# 如何實(shí)現(xiàn)一個帶通知的List<T>
相關(guān)文章
Unity實(shí)現(xiàn)領(lǐng)取獎勵特效
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)領(lǐng)取獎勵特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-10-10C# 獲取硬件參數(shù)的實(shí)現(xiàn)方法
這篇文章主要介紹了C# 獲取硬件參數(shù)的實(shí)現(xiàn)方法的相關(guān)資料,希望通過本文能幫助到大家,讓大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-10-10C#中的小數(shù)和百分?jǐn)?shù)計(jì)算與byte數(shù)組操作
這篇文章介紹了C#中的小數(shù)和百分?jǐn)?shù)計(jì)算與byte數(shù)組操作,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04C#實(shí)現(xiàn)簡單的天氣預(yù)報(bào)示例代碼
這篇文章主要介紹了C#實(shí)現(xiàn)簡單的天氣預(yù)報(bào)示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06超炫酷的WPF實(shí)現(xiàn)Loading控件效果
這篇文章主要介紹了超炫酷的WPF實(shí)現(xiàn)Loading控件效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2015-11-11