开发者

reference types - new keyword to create its value

开发者 https://www.devze.com 2023-02-06 21:53 出处:网络
I believe some literature is not accurate (mayebe even contains mistakes) or its my fault? Book I currently read says (translation):

I believe some literature is not accurate (mayebe even contains mistakes) or its my fault? Book I currently read says (translation):

To creat开发者_JAVA百科e a value of reference type, new keyword has to be used.

I do not think so - what about delegates? Am I correct or do I understand that text badly?


Well, it's not always going to be true, no. Some examples:

  • String concatenation
  • Delegate conversions
  • Boxing conversions

Other examples where new won't appear in your source code would include user-defined operators and conversions.


Delegates are also implicitly created with the new keyword. Behind the scenes, the compiler is generating the necessary code for you. So

MyEvent += SomeMethod;

becomes

MyEvent += new EventHandler(someInstance, SomeMethod);

but it's true, not every time a reference type is created, the new operator is needed. like boxing.


Delegates are reference to a function, and as you probably know, you don't use the "new" keyword when declaring a function.

but you do use it when declaring an object, like:

MyObject obj = new MyObject();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号