What is the purpose of the delegates are immutable in c#?

前端 未结 2 522
南旧
南旧 2020-12-10 17:16

I was reading a book illustrated C # 2012 in the section Combining Delegates point not notice that? Purpose of the delegates are immutable.

Combining

2条回答
  •  情歌与酒
    2020-12-10 17:53

    Delegates are pointers to a method, either concrete or anonymous (well, even anonymous methods are compiled with some compiler-generated identifier).

    Would be reasonable that something that points to some concrete thing be mutable? I don't think so. Each instance represents a pointer to some method. Do you want to point to some other method? Create a new pointer. That is, you instantiate a new delegate.

    In the other hand, the addition of two or more delegates has this result because + operator can be overloaded. That is, delegates can be part of an addition but internally +'s overload is creating a new delegate with an invocation list.

提交回复
热议问题