Are event arguments passed by reference or value in C#?

前端 未结 4 1843
栀梦
栀梦 2021-02-19 08:53

A rather simple question (I think), but I don\'t seem to see an answer already. I know that some values are passed via value (like int and long), and others are passed by refer

4条回答
  •  情深已故
    2021-02-19 09:38

    In the standard event pattern there are two references passed in:

     void FormClosing(object sender, FormClosingEventArgs e) { ... }
    

    those two references are passed 'by value', using for example sender = null will have no effect outside the handling method.

    But you can easily pass a value back:

    void FormClosing(object sender, FormClosingEventArgs e)
    {
        ...
        e.Cancel = true;  // this will pass back to the caller
    }
    

提交回复
热议问题