Predicate<int> IsPositivePred = i => i > 0;
Func<int,bool> IsPositiveFunc = i => i > 0;
new []{2,-4}.Where(i=>IsPositivePred(i)); //Wrap again
new []{2,-4}.Where(IsPositivePred); //Compile Error
new []{2,-4}.Where(IsPositiveFunc); //Func as Parameter
Func<int, int, int> sum = (a, b) => a + b;
Console.WriteLine(sum(3, 5));//Print 8
在本例中,func 没有参数,但返回一个字符串
Func<string> print = () => "Hello world";
Console.WriteLine(print());//Print Hello world
此操作接受两个 int 参数并返回 void
Action<int, int> displayInput = (x, y) => Console.WriteLine("First number is :" + x + " , Second number is "+ y);
displayInput(4, 6); //Print First number is :4 , Second number is :6
这个谓词只有一个参数,并且总是返回 bool。
Predicate<int> isPositive = (x) => x > 0;
Console.WriteLine(isPositive(5));//Print True
public void Example(string str)
{
// The char.IsLetter() function can be used as a predicate
// because it takes 1 char as a parameter, and returns a bool.
// ▼▼▼▼▼▼▼▼▼▼▼▼▼
if (str.All(char.IsLetter))
{
Console.WriteLine("All characters in the string are letters.");
}
}
// Accepts functions that return a decimal type, and...
// ▼▼▼▼▼▼▼
public delegate decimal Operation(decimal left, decimal right);
// ▲▲▲▲▲▲▲ ▲▲▲▲▲▲▲
// ...take 2 parameters, both of type decimal.