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