public delegate bool Predicate (P obj);
using System;
class GFG {
// Method
public static bool myfun(string mystring)
{
if (mystring.Length < 7)
{
return true;
}
else
{
return false;
}
}
// Main method
static public void Main()
{
Predicate val = myfun;
Console.WriteLine(val("a code cat"));
}
}
False
Predicate val = delegate(string str)
{
if (mystring.Length < 7)
{
return true;
}
else
{
return false;
};
val("Geeks");
Predicate val = str = > str.Equals(str.ToLower());
val("a code cat");
Action delegate is used with those methods whose return type is void。
// One input parameter
public delegate void Action < in P > (P obj);
// Two input parameters
public delegate void Action < in P1, in P2 >(P1 arg1, P2 arg2);
using System;
class GFG {
public static void myfun(int p, int q)
{
Console.WriteLine(p - q);
}
static public void Main()
{
Action val = myfun;
val(20, 5);
}
}
Action val = new Action(myfun);
Action val = myfun;
Action val = delegate(string str)
{
Console.WriteLine(str);
};
val(“a code cat”);
Action val = str = > Console.WriteLine(str);
val(“a code cat”);
public delegate TResult Func();
public delegate TResult Func(P arg);
public delegate TResult Func(P1 arg1, P2 arg2);
public delegate TResult Func(P1 arg1, P2 arg2, P3 arg3, P4 arg4, P5 arg5, P6 arg6, P7 arg7, P8 arg8, P9 arg9, P10 arg10, P11 arg11, P12 arg12, P13 arg13, P14 arg14, P15 arg15, P16 arg16);
using System;
class GFG {
public static int method(int num)
{
return num + num;
}
static public void Main()
{
Func myfun = method;
Console.WriteLine(myfun(10));
}
}
Func Delegate 中的最后一个参数始终是一个输出参数,它被视为返回类型。 它通常用于结果
将 Func 委托与匿名方法一起使用
Func
val = delegate(int x, int y, int z)
{
return x + y + z;
};
Func
val = (int x, int y, int z) = > x + y + z;