#357 – Property in Derived Class Hides Base Class Property by Default

If you define a property in a derived class with the same name and type as a property in the base class, the new property hides the base class property by default.  This is true even if you don’t use the new keyword to explicitly indicate that you intend to hide the property in the base class.

If we have a Dog.Temperament property, the following two code snippets are functionally equivalent.

    public class Terrier : Dog
    {
        public new string Temperament
        {
            get
            {
                return string.Format("Terrier {0} is {1}", Name, temperament);
            }
        }

 

    public class Terrier : Dog
    {
        // No new keyword, but we're still hiding base class property
        public string Temperament
        {
            get
            {
                return string.Format("Terrier {0} is {1}", Name, temperament);
            }
        }

Without the new keyword, the compiler warns you that you’re hiding the base class property and recommends using the new keyword.