How do I form a good predicate delegate to Find() something in my List?

前端 未结 6 1540
囚心锁ツ
囚心锁ツ 2020-12-23 12:37

After looking on MSDN, it\'s still unclear to me how I should form a proper predicate to use the Find() method in List using a member variable of T (where T is a class)

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-23 13:02

    Or you can use an anonymous delegate:

    Car myCar = cars.Find(delegate(Car c) { return c.Year == x; });
    
    // If not found myCar will be null
    if (myCar != null)
    {
         Console.Writeline(myCar.Make + myCar.Model);
    }
    

提交回复
热议问题