匿名方法(Anonymous methods) 提供了一种传递代码块作为委托参数的技术。匿名方法是没有名称只有主体的方法。
using System;
namespace CSharpTest01
{
class Program
{
delegate string CallBack(int n);
static void Int2String(CallBack callback, int n)
{
Console.WriteLine("start");
string str = null;
if (callback != null)
{
str = callback(n);
}
Console.WriteLine("{0}\nend", str);
}
static void Main(string[] args)
{
CallBack cb = delegate (int x)
{
string str = x.ToString();
return str;
};
Int2String(cb, 100);
}
}
}