Whats the effect of new keyword in return type of a C# method?
Accidently I actually wrote this method:
public static new iTextSharp.text.Color ITextFocusColor()
{
return new iTextSharp.text.Color(234, 184, 24);
}
It is working as suspected, but I was very surprised that is it allowed to use the new keyword in the return type of the method. Is there any effect/difference if i put开发者_JAVA百科 there new or not? Or what is the new used for?
And is this also possible in other OO-languages?
It doesn't affect the return type, it indicates that this method hides the method from the base class (rather than override it). It can be useful if you need to change the return type declared in the base class, for instance, but the hiding method won't participate in polymorphism, i.e. if you call ITextFocusColor
on an instance of the derived class through a variable of the base class, the base implementation will be called (not the one declared with new
)
See this page on MSDN for more details.
The new
keyword specifies that you are deliberately providing a new implementation of a method or property that is already provided by a base class. It's there to make hiding of base class members a deliberate, self-documenting act.
If your base class doesn't have that method, it doesn't actually matter.
For more details, see the C# specification (specifically, section 3.7.1.2 Hiding through inheritance).
It is the new
modifier. It hides members from a base class instead of overriding.
new
keyword in method return type here hides the parent method.
精彩评论