Featured post
C# - using List<T>.Find() with custom objects -
i'm trying use list<t>
custom class of mine, , being able use methods contains()
, find()
, etc., on list. thought i'd have overload operator ==
apparently, 1 way of doing use delegate method find()
...
note: right now, i've overloaded equals()
method contains()
method work, still couldn't find()
function work.
what best way of getting both work?
i'm using latest c# /.net framework version mono, on linux.
edit: here's code
using system; namespace guerredesclans { public class reponse : iequatable<reponse> { public reponse () { m_statement = string.empty; m_pointage = 0; } public reponse (string statement, int pointage) { m_pointage = pointage; m_statement = statement; } /* * attributs privés */ private string m_statement; private int m_pointage; /* * properties */ public string statement { { return m_statement; } set { m_statement = value; } } public int pointage { { return m_pointage; } set { m_pointage = value; } } /* * equatable */ public bool equals (reponse other) { if (this.m_statement == other.m_statement) return true; else return false; } }
}
and how search reponse objects using find() function...
list.find("statement1"); // return reponse object
find() find element matches predicate pass parameter, not related equals() or == operator.
var element = mylist.find(e => [some condition on e]);
in case, have used lambda expression predicate. might want read on this. in case of find(), expression should take element , return bool.
in case, be:
var reponse = list.find(r => r.statement == "statement1")
and answer question in comments, equivalent in .net 2.0, before lambda expressions introduced:
var response = list.find(delegate (response r) { return r.statement == "statement1"; });
- Get link
- X
- Other Apps
Comments
Post a Comment