Predicate Delegates in C#

后端 未结 10 1401
一个人的身影
一个人的身影 2020-11-27 09:13

Can you explain to me:

  • What is a Predicate Delegate?
  • Where should we use predicates?
  • Any best practices when using predicates?

10条回答
  •  情深已故
    2020-11-27 09:48

    Leading on from Andrew's answer with regards to c#2 and c#3 ... you can also do them inline for a one off search function (see below).

    using System;
    using System.Collections.Generic;
    
    class Program
    {
        static void Main()
        {
            List list = new List { 1, 2, 3 };
    
            List newList = list.FindAll(delegate(int arg)
                               {
                                   return arg> 2;
                               });
        }
    }
    

    Hope this helps.

提交回复
热议问题