Sunday, January 6, 2013

.NET return type of event is void ..why????


An event can have a return value. But it is a BCL guideline to return void (and have 2 parameters).
It becomes a bit murky when you use the multicast property of events, the returned value is the value of the last handler executed. The return of all other subscribed handlers is lost, and you don't have much control over the order in which they are executed. This makes a return value very impractical.
You could write:
delegate int Summer(int[] arr);  // delegate

class Program
{
    public event Summer OnSum;   // event

    void DoSum()
    {
        int[] data = {1, 2, 3} ;

        int sum = OnSum(data);   // execute it.
    }
}

No comments:

Post a Comment

Creating mirror of BST