4.6. Properties

Properties are like smart fields. A property generally has a private data member accompanied by accessor functions and is accessed syntactically as a field of a class. Although properties can have various access levels, the most common and useful access level is public access. You should expose properties instead of public fields from your components. Properties can be versioned, they allow data hiding, and the accessor methods can execute additional logic. Generally, because of JIT optimizations, properties are no more expensive than fields. Properties should be stateless with respect to other properties. They can be set in any order. Properties are discussed further in Chapter 17.

The following keywords are applicable to properties:

  • public. Public properties are accessible to any class. This is the most common use of a property. Properties are usually public, encapsulating the more private field.

  • private. Properties can also be private. This access modifier rarely makes sense for a property, but it is there in case you ever need a private property.

  • protected. Properties can be protected, in which case they are available only to the members of that class as well as its subclass.

  • internal. Internal properties are available to any class of the assembly.

  • override. When a subclass chooses to override a property, it uses the override modifier in front of the property definition.

  • new. When a subclass chooses to hide the superclass's implementation of the property, it uses the new modifier in front of the property definition.

  • abstract. Properties that have no implementation for their get or set or both are considered abstract, and such properties should be defined in an abstract class.

  • static. Properties that belong to a class of objects instead of a specific object instance are called static properties.

4.6.1. Inheritance and Properties

Properties wrap a getter and a setter, and therefore the access modifiers used on a property are applicable to both the get and the set blocks of the property. A subclass can choose to inherit, hide, or override a property of the superclass. A subclass can inherit public, protected, and internal properties. It cannot inherit private properties of the superclass. A subclass that hides a property implementation of the superclass uses the new keyword. Such a subclass can provide a getter or a setter for the property if it is missing in the superclass. So technically, a subclass could convert a read-only superclass property into a mutable one using the new keyword.

A virtual property is like a virtual method. It can be overridden by the subclass. An overridden property cannot provide additional get or set blocks if the superclass does not provide them. That is, a subclass can override only that behavior already provided by the superclass and cannot add new behavior.

A sealed property follows the same rules as a sealed method. A sealed property usually allows a superclass's property implementation to be overridden but does not allow a subclass to override its sealed property.

Listing 4.15 shows an abstract property being defined by the subclass. Note that the subclass cannot provide a setter for the property.

Listing 4.15. Property Inheritance C#
using System;
abstract class SuperClass {
  public abstract int Color {
    get;
  }
}
class SubClass : SuperClass {

  int color;
  public override int Color {
    get {
      return color;
    }
  }
  static void Main(string[] args) {
    SubClass sc = new SubClass();
    Console.WriteLine(sc.Color);
  }
}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.103.183