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:

image

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 learned about abstract classes and members, we discuss how to omit such an implementation ...

Get C# 4.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.