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 #