I've been given a .NET project to maintain. I was just browsing through the code and I noticed this on a property declaration:
public new string navUrl { get { return ...; } set { ... } }
I was wondering what does the new
modifier do to the property?
The new operator creates a new instance of a type. You can also use the new keyword as a member declaration modifier or a generic type constraint.
When used as a declaration modifier, the new keyword explicitly hides a member that is inherited from a base class. When you hide an inherited member, the derived version of the member replaces the base class version.
The new modifier is used to define ShowDetails in the ConvertibleCar class. The override modifier is used to define ShowDetails in the Minivan class.
It is also known as Method Shadowing. In method hiding, you can hide the implementation of the methods of a base class from the derived class using the new keyword. Or in other words, in method hiding, you can redefine the method of the base class in the derived class by using the new keyword.
It hides the navUrl property of the base class. See new Modifier. As mentioned in that MSDN entry, you can access the "hidden" property with fully qualified names: BaseClass.navUrl
. Abuse of either can result in massive confusion and possible insanity (i.e. broken code).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With