在将其分配给委托类型时,我不能在我的方法名称后面添加括号的原因是什么.
这是代码:
public delegate Simple Simple(); //Create a delegate that returns its own type.
class Program
{
public class Exercise
{
public static Simple Welcome()
{
Console.WriteLine("Welcome!");
return null;
}
}
static void Main(string[] args)
{
Simple msg;
msg = Exercise.Welcome(); //Since Welcome returns Simple, I can execute it.
msg();
Console.Read();
}
}
Run Code Online (Sandbox Code Playgroud)
Meh*_*ari 13
它允许编译器将方法调用与对方法组的引用区分开来.如果添加括号,编译器将调用该方法并使用该方法调用的返回值,而不是方法组本身.