3

这段代码有什么问题?

namespace ConsoleApplication1
{
public delegate void del();

class Program
{
    static void Main(string[] args)
    {
        del d = new del(add);
        d += sub;
    }

    public static void add()
    {
        Console.WriteLine("add");
    }

    public static void sub()
    {
        Console.WriteLine("Sub");
    }
  } 
}
4

1 回答 1

2

您需要调用您的委托:

class Program
{
    static void Main(string[] args)
    {
        del d = new del(add);
        d += sub;

        d.Invoke();
    }

    public static void add()
    {
        Console.WriteLine("add");
    }

    public static void sub()
    {
        Console.WriteLine("Sub");
    }
  } 
}
于 2018-02-28T13:12:01.537 回答