Virtual Members

Methods, properties, indexers, and events can be declared as virtual members using the virtual keyword used as a modifier on the member declaration. Virtual members can be overridden in a derived class to specialize their behavior. The simplest example can be found on the System.Object type itself, which is roughly defined as follows (omitting a bunch of irrelevant custom attributes and members):

public class Object{    public virtual bool Equals(object obj) { /* default implementation */ }    public virtual int GetHashCode() { /* default implementation */ }    public virtual string ToString() { /* default implementation */ }    public Type GetType() { /* the one and only implementation */ }    ... ...

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

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.