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

Advertisements

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

#247 – Implementing a Write-Only Property

You implement a write-only property in a class by implementing the set accessor, but not the get accessor.  This will allow client code to write the property’s value, but not read the current value.

Below is an example of a write-only property for the Dog class. The BarkPassword property can be written to, but not read back.  The class then uses the internal value of BarkPassword in the SecureBark method, to verify that the dog is allowed to bark.

        // Internal storage of secret name
        private string barkPassword;

        // Public property, write-only
        public string BarkPassword
        {
            set
            {
                barkPassword = value;
            }
        }

        // Bark method also sets lastBarked
        public void SecureBark(string password)
        {
            if (password == barkPassword)
                Console.WriteLine("{0}: Woof!", Name);
            else
                Console.WriteLine("Not allowed to bark");
        }

Here’s how we might use the new property:

            kirby.BarkPassword = "flotsam62!";

            kirby.SecureBark("notthepassword");

#246 – Implementing a Read-Only Property

You implement a read-only property in a class by implementing the get accessor, but not the set accessor.  This will allow client code to read the property’s value, but not write to it directly.

Below is an example of a read-only property for the Dog class.  The WhenLastBarked property returns a DateTime value indicating the last time that the Dog barked–i.e. the last time that someone called the Bark method.

    // Backing variable storing date/time last barked
    private DateTime lastBarked;

    // Public property, allows reading lastBarked
    public DateTime WhenLastBarked
    {
        get
        {
            return lastBarked;
        }
    }

    // Bark method also sets lastBarked
    public void Bark()
    {
        Console.WriteLine("{0}: Woof!", Name);
        lastBarked = DateTime.Now;
    }

We now have a property in the Dog class that we can read from, but not write to.

    kirby.Bark();
    DateTime whenHeBarked = kirby.WhenLastBarked;

#242 – Declaring and Using a Property in a Class

You can create instance data in a class by defining public fields.  You can read and write fields using a reference to an instance of the class.

A class more often exposes its instance data using properties, rather than fields.  A property looks like a field from outside the class–it allows you to read and write a value using a reference to the object.  But internally in the class, a property just wraps a private field, which is not visible from outside the class.

    public class Dog
    {
        // Private field, stores the dog's name
        private string name;

        // Public property, provides a way to access
        // the dog's name from outside the class
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

    }

Using the property:

            Dog kirby = new Dog();

            kirby.Name = "Kirby";