Predicate Delegates in C#

后端 未结 10 1399
一个人的身影
一个人的身影 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:26

    Simply -> they provide True/False values based on condition mostly used for querying. mostly used with delegates

    consider example of list

    List blabla= new List();
            blabla.Add(new Program("shubham", 1));
            blabla.Add(new Program("google", 3));
            blabla.Add(new Program("world",5));
            blabla.Add(new Program("hello", 5));
            blabla.Add(new Program("bye", 2));
    

    contains names and ages. Now say we want to find names on condition So I Will use,

        Predicate test = delegate (Program p) { return p.age > 3; };
            List matches = blabla.FindAll(test);
            Action print = Console.WriteLine;
            matches.ForEach(print);
    

    tried to Keep it Simple!

提交回复
热议问题