| 发表于:2007-01-29 19:13:29 楼主 |
using system; using system.collections.generic; using system.text; namespace _csharp_console { class employee { string name; string position; public string name { get { return name; } } public string position { get { return position; } } public employee(string name, string position) { this.name = name; this.position = position; } public override string tostring() { return "name: " + name + "\tposition: " + position; } } class program { static void main(string[] args) { list <employee> emplist = new list <employee> (); emplist.add(new employee( "name1 ", "ceo ")); emplist.add(new employee( "name2 ", "cto ")); emplist.add(new employee( "name3 ", "cfo ")); emplist.add(new employee( "name4 ", "coo ")); emplist.add(new employee( "name5 ", "ceo ")); console.writeline(emplist.find(findone)); console.readline(); } public static bool findone(employee e) //#1 { if (e.position == "coo ") return true; return false; } } } 看msdn中关于list <t> .find()的解释说搜索与指定谓词所定义的条件相匹配的元素,并返回整个 list 中的第一个匹配元素。 public t find ( predicate <t> match ) 参数 match predicate 委托,用于定义要搜索的元素的条件。 返回值 如果找到与指定谓词定义的条件匹配的第一个元素,则为该元素;否则为类型 t 的默认值。 我所不明白的是关于返回值这,为什么#1处的函数的返回值类型必须定义为bool呢,我觉得应该返回employee类型才对啊.我也就是不明白为什么函数里还返回ture或false.还有为什么findone函数要有employee e这个形参呢? |
|
|
|
|