为代理分配方法时,为什么不添加parantheses?

Xai*_*oft 1 c# delegates

在将其分配给委托类型时,我不能在我的方法名称后面添加括号的原因是什么.

这是代码:

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

它允许编译器将方法调用与对方法组的引用区分开来.如果添加括号,编译器将调用该方法并使用该方法调用的返回值,而不是方法组本身.

  • 参考方法*组*.这种区别很重要,因为方法可能会有重载. (2认同)