Examples of Predicateless thanTgreater than and Funcless thanT boolgreater than generic delegates in C

  • 2020-10-07 18:51:43
  • OfStack

This article analyzes Predicate in C# as an example < T > With Func < T, bool > The usage of generic delegate is Shared for your reference. The details are as follows:

Take a look at the following example:


static void Main(string[] args)  
{  
  List<string> l = new List<string>();  
  l.Add("a");  
  l.Add("b");  
  l.Add("s");  
  l.Add("t");  
 
  if (l.Exists(s => s.Equals("s")))  
  {  
    string str = l.First(s => s.Equals("s"));  
    Console.WriteLine(str);  
  }  
  else 
    Console.WriteLine("Not found");  
}  

Quite simply, you first determine if there is an s string in the string list l, and if there is, you take it and display it. As you can see from the code, the l.Exists method and l.First method use the same parameters, but is this really the case?

In fact, List < T > Exists and List < T > . The parameters of First use different delegates:
Predicate < T > And Func < T, bool > . From the signature of the function, there is no difference between the two functions. Both refer to the functions with the parameter type of T and the return value of bool, but after all, they belong to different delegate types. Therefore, the following code obviously cannot be compiled:


static void Main(string[] args)  
{  
  List<string> l = new List<string>();  
  l.Add("a");  
  l.Add("b");  
  l.Add("s");  
  l.Add("t");  
  Func<string, bool> p = s => s.Equals("s");  
  if (l.Exists(p))  
  {  
    string str = l.First(p);  
    Console.WriteLine(str);  
  }  
  else 
    Console.WriteLine("Not found");  
}  

However, thanks to Predicate < T > And Func < T, bool > It does refer to functions of the same class with the same signature, and we often don't want to write the body of an anonymous method twice to assign Predicate to each < T > And Func < T, bool > Generic delegate, so we can write our own extension method to extend Func < T, bool > Type to make it easy to convert to Predicate < T > Type:


public static class Extensions  
{  
  public static Predicate<T> ToPredicate<T> (this Func<T, bool> source)
  {  
    Predicate<T> result = new Predicate<T>(source);  
    return result;  
  }  
}  

With this extension introduced, our code can be written as follows:


static void Main(string[] args)  
{  
  List<string> l = new List<string>();  
  l.Add("a");  
  l.Add("b");  
  l.Add("s");  
  l.Add("t");  
  Func<string, bool> p = s => s.Equals("s");  
  if (l.Exists(p.ToPredicate()))  
  {  
    string str = l.First(p);  
    Console.WriteLine(str);  
  }  
  else 
    Console.WriteLine("Not found");  
}  

To be honest, I don't know why MS implements the Exists and First methods with two completely different generic delegates, which makes the code relatively complex and error prone in some cases. I think for the sake of semantic clarity, Exists is just making judgments, so you need to use assertion expressions, whereas First is more about iteratively calling a specified method. There is no end to learning.

Hopefully this article has helped you with your C# programming


Related articles: