C# Use object's property rather than reference for List.Contains() -
i have like:
public class myclass { public string type { get; set; } public int value { get; set; } } and have:
list<myclass> mylist = new list<myclass>() // ... populate mylist if (mylist.contains("testtype")) { // } in above code, want mylist.contains() match on type property rather myclass object reference. how achieve this? use icomparable or icompare interface, override myclass.equals(), or sufficient override string cast of myclass?
edit: after doing tests overriding equals() , string cast of myclass, implementing icompare , icomparable, have found none of these methods work. actually, seems work if override myclass cast of string, mylist.contains((myclass)"testtype"). think scrum meister's answer better :)
you can use any extension method:
if (mylist.any(x => x.type == "testtype")) { // }
Comments
Post a Comment