【发布时间】:2013-02-04 00:24:55
【问题描述】:
我有以下几点:
class Program {
delegate int myDelegate(int x);
static void Main(string[] args) {
Program p = new Program();
Console.WriteLine(p.writeOutput(3, new myDelegate(x => x*x)));
Console.WriteLine("press [enter] to exit");
Console.ReadLine();
}
private string writeOutput(int x, myDelegate del) {
return string.Format("{0}^2 = {1}",x, del(x));
}
}
上面的方法writeOutput是必须的吗?可以在没有writeoutput 的情况下重写以下内容以输出与上述相同的内容吗?
可以修改Console.WriteLine("x^2 = {0}", new myDelegate(x => x*x)); 行,以便将 3 输入到函数中吗?
class Program {
delegate int myDelegate(int x);
static void Main(string[] args) {
Program p = new Program();
Console.WriteLine("x^2 = {0}", new myDelegate(x => x*x));
Console.WriteLine("press [enter] to exit");
Console.ReadLine();
}
}
【问题讨论】:
-
除非您正在练习使用委托,否则我不明白为什么在您的代码中需要使用委托。您拥有价值,并且知道如何处理它。
-
@AndersonSilva - 第一次就对了 - 我正在研究代表和 lambda 函数