Hey, I wondered why is it that the return type of events such as
private void button1_Click(object sender, EventArgs e)
is always void?
In c#, Events could be of two types
1. Multicast
2. UnitCast
Multicast event are those who has more than one subscriber. when a multicast event is raised, then multiple handler would be invoked because you have subscribed more than one event handler. So, multicast event are intended to be used for calling multiple handler and multicast handler can't have return type why??
because if multicast delegate has return type, then every event handler would return some value and return value of one event handler would be replaced by the next event handler value.
suppose you have a multicast delegate as following
public delegate int buttonClick;
public event buttonClick onClick;
onClick += method1
onclick += method2
onclick += metho3
when this event would be raised then value returned by method1 will be replaced by value return by method2 and eventually value of only method3 would be received.
Therefore, in case of Multicast delegate, it is always recommend not to return any value.
but in case of unicast deleagte wherein you would have only one subscriber. So you can return value for fulfilling your purpose
So, for multicast delegate - No return type and for unicast delegate - can have return type
Multicast delegate can also return multiple values but for that event has to be raised manually.
Event in case you opt that multicast delegate should also return values, lets say I am having an event which is bind to 4 event handler and it takes two integers and one handler does add, second subtraction and third multiply and last divide. So, if you want to get the return type of all the handlers, then you have to raise the event manually in the following manner,
var handler = _eventToRaised.GetInvocationList();
foreach(var handler in handlers)
{
if(handler != null)
{
var returnValue = handler()// pass the values which delegate expects.
}
}