reference types - new keyword to create its value
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();
精彩评论