标签:返回 result 回调 public 应用程序 构造函数 stat toolbar 定义
转:https://www.cnblogs.com/wyongqi/p/7483748.html
如果我们希望定义一个委托类型来调用任何返回void并且接受单个参数的方法。如果这个参数可能会不同,我们就可以通过类型参数来构建。
下面我们看一个小示例:
public class GenericDelegate
{
public delegate void MyGenericDelegate<T>(T arg);
public static void Show()
{
// 注册目标
MyGenericDelegate<string> stringTarget = new MyGenericDelegate<string>(StringTarget);
stringTarget("i am ok");
MyGenericDelegate<int> intTarget = new MyGenericDelegate<int>(IntTarget);
intTarget.Invoke(100);
}
static void StringTarget(string arg) => Console.WriteLine($"StringTarget--> {arg.ToUpper()}");
static void IntTarget(int arg) => Console.WriteLine($"IntTarget--> {++arg}");
}
a. 泛型Action<> 和 Func<> 委托
从以上的学习中我们已经了解到,使用委托在应用程序中进行回调需要遵循以下步骤:
其实,这种方式通常会构建大量只用于当前任务的自定义委托。当委托名无关紧要的时候,我们可以使用框架内置的Action<> 和 Func<> 泛型委托,可指向至多传递16个参数的方法。
Action<>:无返回值: 定义 public delegate void Action<...>
public class MyActionDelegate
{
public static void Show()
{
// 使用Action<>委托来指向 DisplayMessage()
Action<string, ConsoleColor, int> actionTarget = new Action<string, ConsoleColor, int>(DisplayMessage);
actionTarget("actionTarget", ConsoleColor.Red, 5);
}
// Action<> 委托的一个目标
private static void DisplayMessage(string msg, ConsoleColor txtColor, int printCount)
{
ConsoleColor previous = Console.ForegroundColor;
Console.ForegroundColor = txtColor;
for (int i = 0; i < printCount; i++)
{
Console.WriteLine(msg);
}
Console.ForegroundColor = previous;
}
}
运行效果如下图:

Func<>:有返回值 public delegate TResult Func<..., out TResult>
Func的 参数列表中,最后一个是返回类型,前面的都是参数类型
public class FuncDelagate
{
public static void Show()
{
Func<int, int, int> funcTarget = new Func<int, int, int>(Add);
int result = funcTarget(1, 2);
Console.WriteLine(result);
Func<int, int, string> funcTarget2 = new Func<int, int, string>(SumToString);
string sumStr = funcTarget2(3, 4);
Console.WriteLine(sumStr);
}
static int Add(int x, int y) => x + y;
static string SumToString(int x, int y) => (x + y).ToString();
}
运行结果:3,7
鉴于 Action<> 和 Func<> 节省了手工创建自定义委托的步骤,but 总是应该使用他们吗?
答案:“视情况而定”。
很多情况下 Action<> 和 Func<> 都是首选,但如果你觉得一个具有自定义名称的委托更有助于捕获问题范畴,那么构建自定义委托不过就是一行代码的事儿。
注:Linq中就大量的用到了 Action<> 和 Func<>。
标签:返回 result 回调 public 应用程序 构造函数 stat toolbar 定义
原文地址:https://www.cnblogs.com/wangle1001986/p/12179729.html