Action<string> print = Print; print("使用print打印");
Func<float,float,float> fc = Mul; var f = fc(3, 4).ToString(); Console.WriteLine(f);
CalFunc func = Add; func(1.1f, 2);
Console.WriteLine("加入Sub()"); func += Sub; func(1.1f, 2);
Console.WriteLine("移除Add()"); func -= Add; func(1.1f, 2);
Console.WriteLine("-------------闭包↓----------------"); int intVal = 10; Action aa = () => { intVal += 10; }; aa(); Console.WriteLine(intVal);
static void Print(string info) =>Console.WriteLine(info);
delegate float CalFunc(float f1, float f2); static float Add(float arg1, float arg2) { Console.WriteLine($"加法{arg1}+{arg2}={arg1 + arg2}"); return arg1 + arg2; } static float Sub(float arg1, float arg2) { Console.WriteLine($"减法{arg1}-{arg2}={arg1 - arg2}"); return arg1 - arg2; } static float Mul(float arg1, float arg2) { Console.WriteLine($"乘法{arg1}*{arg2}={arg1 * arg2}"); return arg1 * arg2; }
|