delegate、Action/Func 与闭包
通过委托传递行为:声明自定义委托,使用内置的 Action/Func,并了解 lambda 如何捕获变量(闭包)。
delegate、Action/Func 与闭包 是 CoddyKit 上的免费 C# Academy 课时。 这是第 1 节课,共 3 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 C# Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 C# Academy 课程共包含 3 节课。
委托概览
目标:将方法视为数据。
- 定义委托类型
- 使用 Action/Func
- 创建 lambda 表达式
- 理解闭包(捕获的变量)
自定义委托基础
委托是一种类型安全的函数指针。为它分配兼容的方法,并像调用函数一样调用它。
using System;
// Custom delegate type: a method taking two ints and returning int
public delegate int Op(int x, int y);
public class Program
{
static int Add(int a, int b) { return a + b; }
static int Mul(int a, int b) { return a * b; }
public static void Main(string[] args)
{
Op f = Add; // method group conversion
Console.WriteLine(f(2, 3)); // 5
f = Mul;
Console.WriteLine(f(2, 3)); // 6
}
}
Action 与 Func
Action 表示无返回值的方法;Func 返回一个值。请使用方法组或 lambda 表达式。
using System;
public class Program
{
static void Log(string s) { Console.WriteLine("LOG: " + s); }
public static void Main(string[] args)
{
Action<string> a = Log; // no return
a("hello");
Func<int, int, int> sum = delegate(int x, int y) { return x + y; }; // lambda/anon method
Console.WriteLine(sum(3, 4)); // 7
// Method group to Func
Func<string, int> len = GetLen;
Console.WriteLine(len("abc")); // 3
}
static int GetLen(string s) { return s == null ? 0 : s.Length; }
}
闭包演示
闭包会捕获外部作用域中的变量。该变量的生命周期会超过创建委托的方法。
using System;
public class Program
{
public static void Main(string[] args)
{
int counter = 0; // captured variable (lives as long as delegate)
Action inc = delegate() { counter = counter + 1; Console.WriteLine(counter); };
inc(); // 1
inc(); // 2
Console.WriteLine("Final=" + counter); // 2
}
}
高阶方法
将 Func 或 Action 传入方法,以自定义行为。这是许多 API(以及 LINQ)的基础。
using System;
public class Program
{
// Apply a transform to each number and sum the results
static int SumBy(int[] xs, Func<int, int> transform)
{
int total = 0;
for (int i = 0; i < xs.Length; i++)
{
total += transform(xs[i]);
}
return total;
}
public static void Main(string[] args)
{
int[] data = new int[] { 1, 2, 3 };
int sumSquares = SumBy(data, delegate(int x) { return x * x; });
Console.WriteLine(sumSquares); // 14
}
}
委托提示
提示:
- 优先使用 Action/Func,以避免额外的自定义委托类型。
- 让 lambda 表达式保持简短清晰。
- 注意捕获的变量;它们的值之后可能发生变化。
- 如果已有命名方法符合签名,请使用方法组。
Func 与 Action 的含义
总结
总结:声明委托,使用 Action/Func,并编写简短的 lambda 表达式。闭包让 lambda 表达式能够捕获局部变量,请谨慎使用。
常见问题解答
「delegate、Action/Func 与闭包」课时是免费的吗?
是的 — 「delegate、Action/Func 与闭包」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 C# Academy 课程的其余内容,请升级到 CoddyKit PRO。 C# Academy 课程共包含 3 节课。
「delegate、Action/Func 与闭包」这节课中我会学到什么?
通过委托传递行为:声明自定义委托,使用内置的 Action/Func,并了解 lambda 如何捕获变量(闭包)。 你通过在浏览器中直接运行的动手代码来练习 C# Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 C# Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 C# Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 3 节。
「delegate、Action/Func 与闭包」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 C# Academy 课中编写并运行代码吗?
能。每节 C# Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- delegate、Action/Func 与闭包
- 事件:发布/订阅与 event 关键字
- 事件模式与常见陷阱(内存泄漏)