首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

用List<T>的Find方法有点不明白,该怎么解决

2012-04-05 
用ListT的Find方法有点不明白usingSystemusingSystem.Collections.GenericusingSystem.Textnamespace

用List<T>的Find方法有点不明白
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这个形参呢?

[解决办法]
FindOne 返回值是定义一个方法的结果
是为了以后做判断方便
比如
if (FindOne(x)) bool 型好判断
如果你要返回会对象 也没什么问题

之所以有 Employee 类型做参数
是因为 List 里面存的就是 Employee
而不是存的 Employee 的一个属性
[解决办法]
我所不明白的是关于返回值这,为什么#1处的函数的返回值类型必须定义为bool呢,我觉得应该返回Employee类型才对啊
------------------------------------------
1)Predicate 为
public delegate bool Predicate <T> (
T obj)
委托格式要求必须返回 bool
2) 如果你不想使用Find方法,你完全可以这样写
public static Employee FindOne(List <Employee> list) //#1
{
foreach(Employee e in list)
{
if(e.Position== "C00 ")
{
return e;
}
}
return null;
}
然后调用的时候
Console.WriteLine(FindOne(empList));
你这样,就不再需要Find函数了。因为你可以看到这个,在msdn上
此方法执行线性搜索;因此,此方法的运算复杂度为 O(n),其中 n 是 Count。但是,你看这样修改后的代码量和原来比哪个多?


2。还有为什么FindOne函数要有Employee e这个形参呢?
------------------------
public delegate bool Predicate <T> (
T obj
)
这个委托就是这么定义的


热点排行