您的位置:程序门 -> .net技术 -> c#



用list<t>的find方法有点不明白


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


用list<t>的find方法有点不明白[已结贴,结贴人:china3c2]
发表于: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这个形参呢?
发表于:2007-01-29 19:22:181楼 得分:10
findone   返回值是定义一个方法的结果
是为了以后做判断方便
比如
if   (findone(x))   bool   型好判断
如果你要返回会对象   也没什么问题

之所以有   employee   类型做参数
是因为   list   里面存的就是   employee
而不是存的   employee   的一个属性
发表于:2007-01-29 19:23:162楼 得分:0
多在实例中用
很多问题就迎刃而解了
概念将多了反而会晕的
发表于:2007-01-29 19:31:203楼 得分:20
我所不明白的是关于返回值这,为什么#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
)
这个委托就是这么定义的


发表于:2007-01-29 20:11:334楼 得分:0
public   static   bool   findone(employee   e)                     //#1
                {
                        if   (e.position   ==   "coo ")
                                return   true;
                        return   false;
                }

看看这个,我在单步调式的时候发现,e从list头遍历到list尾,可程序中我没看出哪有把e与list关联起来的语句啊,有点不明白了.
发表于:2007-01-29 20:21:045楼 得分:0
明白了
发表于:2007-01-29 20:26:576楼 得分:0
predicate <t>   match是一个委托,它的方法签名为:
public   delegate   bool   predicate <t>   (
t   obj
)

而这个方法:
public   static   bool   findone(employee   e)                     //#1
                {
                        if   (e.position   ==   "coo ")
                                return   true;
                        return   false;
                }
正好是满足委托的方法签名的(即返回值都是bool型,都只有一个参数,且类型为t)..


快速检索

最新资讯
热门点击