日期:2014-05-18  浏览次数:20765 次

如何使用 泛型中的remove 和find 方法
做项目的时候遇到个问题,想了很久都没解决,说出来看大家有没有办法

public   class   testClass
        {
                private   string   _testid   =   " ";
                private   string   _testname   =   " ";

                public   string   testid
                {
                        get   {   return   this._testid;   }
                        set   {   this._testid   =   value;   }
                }
                public   string   testname
                {
                        get   {   return   this._testname;   }
                        set   {   this._testname   =   value;   }
                }
        }
        public   class   testCollection   :   List <testClass>
        {   }

问题是这样的,现在我已经得到了一个testCollection   实例,   名字叫MyColl,我看到MyColl中有remove,removeat,find   等方法,我想在MyColl中找到testID=2的元素,然后删除他,大家能不能谈谈该怎么实现?好象是要用到委托,但我对委托一直不明白

------解决方案--------------------
just sample...

MyColl.Remove(MyColl.Find(FindById));

private static bool FindById(testClass match)
{
return match.testid == 2;
}

------解决方案--------------------
因为 Predicate 泛型委托 只允许一个参数...

public delegate bool Predicate <T> (T obj)