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

#356 – Hidden Base Class Property Is Used Based on Declared Type of Object

When you use the new modifier to hide a base class property, its property accessors will still be called by objects whose type is the base class.  Objects whose type is the derived class will use the new property in the derived class.

            Dog kirby = new Dog("Kirby", 15);
            kirby.Temperament = "EAGER";
            Console.WriteLine(kirby.Temperament);  // Kirby is eager

            Terrier jack = new Terrier("Jack", 15);
            jack.Temperament = "CraZY";
            Console.WriteLine(jack.Temperament);  // Terrier Jack is CRAZY

We could also use a variable of type Dog (the base class) to refer to an instance of a Terrier (the derived class).  If we then reference the Temperament property using this base class variable, the Temperament property in the base class is used, even though we’re working with an instance of the derived class.

            Dog kirby = new Dog("Kirby", 15);
            kirby.Temperament = "EAGER";      // Uses Dog.Temperament

            Dog jack = new Terrier("Jack", 15);
            jack.Temperament = "CraZY";       // Also uses Dog.Temperament

#355 – Use the new Keyword to Replace a Property in a Base Class

A derived class inherits data and behavior from its parent class.

There are times when you might want to replace property accessors in a base class with new accessors in the derived class, using the same property name.  You can do this using the new keyword.

Assume a Dog class has a Temperament property:

        protected string temperament;
        public string Temperament
        {
            get
            {
                return string.Format("{0} is {1}", Name, temperament);
            }

            set
            {
                temperament = value.ToLower();
            }
        }

You can provide a new version of this property in a class that derives from Dog, using the new keyword.  This new property hides the property in the base class.

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

            set
            {
                temperament = value.ToUpper();
            }
        }

The get/set accessors used will now depend on the type of the object.

#339 – Readonly Fields vs. Read-Only Properties

A read-only property in a class is similar to a readonly field.  Both expose a value that users of the class can read, but not write.

A readonly field is defined using the readonly modifier.  A read-only property is defined by including a get accessor for the property, but not a set accessor.

A readonly field can have only a single value, set either at the time that the field is declared, or in a constructor.  A read-only property returns a value that may be different each time the property is read.

Example of a readonly field:

        // Readonly field, initialized in constructor
        public readonly string OriginalName;

        public Dog(string name)
        {
            Name = name;

            // Set readonly field once, in the constructor
            OriginalName = name;
        }

Example of a read-only property:

        public string FormalName
        {
            get
            {
                return string.Format("Sir {0}", Name);
            }
        }

#311 – Accessibility of Properties in a Class

You can apply access modifiers to properties defined in a class to define their accessibility.  Accessibility dictates what other code is allowed to read and the write the value of a property.

  • public – All code can read/write the property
  • private – Only code in the defining class can read/write the property
  • protected – Code in the defining class or derived classes can read/write the property
  • internal – All code in the defining assembly can read/write the property
  • protected internal – Code in the defining assembly or in derived classes can read/write the property
    public class Dog
    {
        // All code can access
        public string Nickname { get; set; }

        // Only code in this calss can access
        private string genericDogSecretName { get; set; }

        // Code in this class or subclass can access
        protected int totalBarkCount { get; set; }

        // Code in same assembly can access
        internal int invokeCount { get; set; }

        // Code in same assembly or derived classes can access
        protected internal int barkInvokeCount { get; set; }
    }

#261 – How Automatic Properties Look Under-the-Covers

The typical pattern for implementing an automatic property in a C# class is shown below–you do not need to define either a private backing variable, or the bodies of the get and set accessors.

    public class Dog
    {
        // An automatic property
        public string Name { get; set; }
    }

It’s interesting to use the IL DASM tool to see how this property is actually implemented.  Start up the IL Disassembler tool and then do a File | Open and load the .exe containing the property shown above.  You’ll see the following:

You can see the backing variable that the compiler automatically generated–k__BackingField.  You can also see the get and set accessors that the compiler automatically created, get_Name and set_Name.

#260 – How Properties Look Under-the-Covers

The typical pattern for implementing a property in a C# class is shown below–you define a private backing variable in which to store the property value, as well as get and set accessors that read/write the property value.

    public class Dog
    {
        // An typical instance property
        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
    }

We can use the IL DASM tool to take a look at how this property is actually implemented.  Start up the IL Disassembler tool and then do a File | Open and load the .exe containing the property shown above.  You’ll see the following:

We see our private backing variable–name–as well as two methods that represent the get/set accessors–get_Name and set_Name. These are the methods that the compiler generates, which implement the accessors.

#259 – Static vs. Instance Properties

A typical property declared in a class is an instance property, meaning that you have a copy of that property’s value for each instance of the class.

You can also define static properties, which are properties that have a single value for the entire class, regardless of the number of instances of the class that exist.

    public class Dog
    {
        // An instance property--one copy for each dog
        public string Name { get; set; }

        // A static property--one copy for all dogs
        public static string Creed { get; set; }
    }

You can read and write a static property even if no instances of the class exist.  You use the class’ name to reference a static property.

            // Writing an instance property  (Name)
            Dog kirby = new Dog();
            kirby.Name = "Kirby";

            Dog jack = new Dog();
            jack.Name = "Jack";

            // Write a static property
            Dog.Creed = "We are best friends to humans.";

#254 – Implementing a Read-Only Property with a Private Set Accessor

One way to implement a read-only property is to provide only a get accessor, letting the code internal to the class write the property’s value by writing directly to a private backing variable.

        private string personalHowl;
        public string PersonalHowl
        {
            get { return personalHowl; }
        }

However, it still might be helpful for code in the class to have a set accessor, which serves as a single place to execute whatever code is required when writing the property’s value.

You can use the private access modifier to make the set accessor available only to code within the class.  The property will appear to be read-only from code outside of the class.

        private string personalHowl;
        public string PersonalHowl
        {
            get { return personalHowl; }

            private set { personalHowl = value.ToUpper(); }
        }

#253 – Implementing a Read-Only Automatic Property

When using the automatic property syntax,C# requires that you define both a get and a set accessor.  But with the default syntax, you end up with a read/write property.

        public int Age { get; set;  }

But what if we want a read-only property, from the client code’s perspective?  Since we’re required to define the set accessor, it looks like client code would always be able to write to the property.

The solution is that we can add a private access modifier to the set accessor.

        public int Age { get; private set; }

With the set accessor private, code external to the class can read the property’s value, but not write to it.  But code within the class can still write to the property.