tags:

views:

117

answers:

4

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?

+10  A: 

It hides the navUrl property of the base class. see new Modifier. As mentioned in the MSDN entry you can access the "hidden" property with fully qualified names: BaseClass.navUrl. Abuse of either can result in massive confusion an possible insanity...i.e. broken code.

Rusty
+3  A: 

this is hiding the property

might be like this your code

class base1
        {

            public virtual string navUrl
            {
                get;
                set;
            }

        }

        class derived : base1
        {
            public new string navUrl
            {
                get;
                set;
            }

        }

here in the derived class navUrl property is hiding base class property.

anishmarokey
+1  A: 

Some times referred to as Shadowing or method hiding; The method called depends on the type of the reference at the point the call is made. This might help.

KMan
+1  A: 

This is also documented here.

Code snippet from msdn.

public class BaseClass
{
    public void DoWork() { }
    public int WorkField;
    public int WorkProperty
    {
        get { return 0; }
    }
}

public class DerivedClass : BaseClass
{
    public new void DoWork() { }
    public new int WorkField;
    public new int WorkProperty
    {
        get { return 0; }
    }
}    

DerivedClass B = new DerivedClass();
B.WorkProperty;  // Calls the new property.

BaseClass A = (BaseClass)B;
A.WorkProperty;  // Calls the old property.
Santhosh