Why are delegates reference types?

前端 未结 7 726
星月不相逢
星月不相逢 2020-12-04 19:28

Quick note on the accepted answer: I disagree with a small part of Jeffrey\'s answer, namely the point that since Delegate had to be a referenc

7条回答
  •  余生分开走
    2020-12-04 19:55

    Imagine if delegates were value types.

    public delegate void Notify();
    
    void SignalTwice(Notify notify) { notify(); notify(); }
    
    int counter = 0;
    Notify handler = () => { counter++; }
    SignalTwice(handler);
    System.Console.WriteLine(counter); // what should this print?
    

    Per your proposal, this would internally be converted to

    struct CompilerGenerated
    {
        int counter = 0;
        public Execute() { ++counter; }
    };
    
    Notify handler = new CompilerGenerated();
    SignalTwice(handler);
    System.Console.WriteLine(counter); // what should this print?
    

    If delegate were a value type, then SignalEvent would get a copy of handler, which means that a brand new CompilerGenerated would be created (a copy of handler) and passed to SignalEvent. SignalTwice would execute the delegate twice, which increments the counter twice in the copy. And then SignalTwice returns, and the function prints 0, because the original was not modified.

提交回复
热议问题