C# 委托

Action 和 Func

action没有返回值。

func有返回值。

Action<T> 

// Action<T> 是系统预定义的委托类型
public delegate void Action<in T>(T obj);  // 简化表示

// 所以 Action<object> 相当于:
public delegate void MyAction(object obj);  // 接收object参数,无返回值

2. 不同形式的Action委托

// 无参数
Action action1 = () => Console.WriteLine("无参数");

// 一个参数
Action<string> action2 = (s) => Console.WriteLine(s);  // string参数
Action<object> action3 = (o) => Console.WriteLine(o);  // object参数

// 多个参数
Action<int, string> action4 = (i, s) => Console.WriteLine($"{i}:{s}");
Action<int, int, int> action5 = (a, b, c) => Console.WriteLine(a + b + c);

 

posted @ 2026-03-15 00:40  Xia.CJ  阅读(15)  评论(0)    收藏  举报