Declaring Virtual Members

To illustrate the concept of virtual members from a different angle, let’s define another type hierarchy in which we declare virtual members ourselves (rather than overriding the ones from System.Object). A good example is the concept of a Shape that can be queried for its area and circumference. Depending on the subclass of Shape, calculation of those values will differ:

class Shape{    public virtual double Area    {        get { return 0.0; }    }    public virtual double Circumference    {        get { return 0.0; }    }}

For the time being, we use default implementations for those virtual property getters, which don’t look as if they produce very meaningful values. Later on, after you’ve ...

Get C# 5.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.