C Listless thanTgreater Containsless thanTgreater than of
- 2020-06-12 10:30:01
- OfStack
Generic List < T > Contains is a method that compares whether a list already contains an object < T > Today I searched 1 usage on the Internet write it down for future reference.
To compare our custom objects in this way, we first need a comparator,
Note that the comparator here is implementation IEqualityComparer < T > For interfaces, don't write IComparer < T > .
As follows:
/// <summary>
/// tracing The: popup model object list comparator ( According to the ID To compare )
/// </summary>
public class PopupComparer : IEqualityComparer<Model.PopupModel.PopupModel>
{
public static PopupComparer Default = new PopupComparer();
#region IEqualityComparer<PopupModel> Members of the
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
}
Then we can call Contains < T > () The method is as follows:
List<PopupModel> list = new List<PopupModel>();
//model Is the object to be compared
if(list.Contains<PopupModel>(model,PopupComparer.Default))
{
}