你有没有想过像调用变量一样调用函数,把函数当做变量一样传递。
定义委托
delegate |
仔细看一下上面的定义,会发现这很像一个函数。
下面演示一下委托的简单的应用。
using System; namespace ConsoleApplication3 { class { delegate static { delegateF f = new int sum = f(1,2); Console.WriteLine(sum); } static { return a + b; } } } |
下面的例子演示函数作为变量传递
using System; namespace ConsoleApplication3 { class { delegate static { delegateF f = new Wsum(f,1,2); Wsum(Add,1,2); } static { return a + b; } static { int sum = f(a,b); Console.WriteLine(sum); } } } |
2021年11月5日