Examples of usages of predicate<t> and func<t, bool> generic delegates in C #

Source: Internet
Author: User

This paper analyzes the use of predicate<t> and func<t, bool> generic Delegate in C # By example, and shares it for everyone's reference. Specific as follows:

Let's take a look at the following example:

12345678910111213141516 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"); }

It is very simple to determine if there is an S string in the string list L, and if so, take it and display it. As you can see from the code, the arguments used by the L.exists method and the L.first method are the same, but is the fact true?

In fact, List<t> exists and list<t>. The first parameter uses a different delegate for each:
Predicate<t> and Func<t, bool>. From the signature of the function, there is no difference between the two refers to the parameter type T, the return value is a bool function, but after all, the two belong to a different delegate type, so the following code is obviously unable to compile through:

12345678910111213141516 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, because of predicate<t> and func<t, Bool> does refer to the same class with the same signature function, and we often do not want to repeat the method body of the anonymous method two times to give Predicate<t> and func<t, bool> generic delegate, so we can write ourselves an extension method, extend Func<t, bool> type to make it easy to convert to predicate<t> type:

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

After introducing this extension method, our code can be written in the following form:

12345678910111213141516 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"); }

Honestly, I don't know why MS is implementing the exists and first methods with two completely different generic delegates, which makes the code relatively complex and even error-prone in some cases. I think for the sake of semantic clarity, exists is merely a judgment, so it is necessary to use an assertion expression, and in doing the first operation, it is more of an iterative invocation of the specified method. We need to continue to explore.

I hope this article is helpful to everyone's C # programming.

Examples of usages of predicate<t> and func<t, bool> generic delegates in C #

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.