Why do event handlers always have a return type of void?

前端 未结 11 2144
夕颜
夕颜 2020-12-05 23:30

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?

11条回答
  •  半阙折子戏
    2020-12-06 00:03

    The return type is void because it's a sub routine, not a function. You could have it return a value, but event handlers (which is what a sub routine hooked to a button click event is) aren't exactly intended to.

    In VB, this line of code would be:

    Private Sub button_Click(ByVal sender As Object, ByVal e As EventArgs)
    

    The explicit "Sub" statement in VB makes a little more sense in this case, but remember, all voids in C# are just subroutines ... they do something in the code based on arguments but don't return a value. They can, however, change the values of the arguments passed in.

提交回复
热议问题