Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"new" keyword in property declaration in c#

Tags:

c#

.net

asp.net

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?

like image 284
Ben Rowe Avatar asked Sep 06 '10 05:09

Ben Rowe


People also ask

What is the use of new keyword in C?

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.

What is the use of new keyword in inheritance in C#?

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.

What is the difference between new and override?

The new modifier is used to define ShowDetails in the ConvertibleCar class. The override modifier is used to define ShowDetails in the Minivan class.

Where have you used new keyword Apart from creating a new instance or what is method hiding shadowing?

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.


1 Answers

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).

like image 185
Rusty Avatar answered Sep 19 '22 12:09

Rusty