Virtual Members

A virtual (Overridable in Visual Basic) member allows you to change a member's behavior by providing a different implementation of the member. They are typically used when you want a derived class of the type to handle the specifics of a given scenario. For example, the WebRequest class defines functionality for sending a request to any Universal Resource Identifier (URI). The FtpWebRequest class is a derived class of WebRequest that overrides its virtual methods to handle sending requests to URIs that use the File Transfer Protocol (FTP) scheme.

Virtual members perform better than callbacks and events, but do not perform better than non-virtual methods.

Do not make members virtual unless you have a good reason to do so and you are aware of all the costs related to designing, testing, and maintaining virtual members.

Changing the implementation of a virtual member between versions can cause subtle version incompatibilities. For this reason, virtual methods are most costly to design correctly and test thoroughly.

Do prefer protected accessibility over public accessibility for virtual members. Public members should provide extensibility (if required) by calling into a protected virtual member.

Members needed for all scenarios that do not involve inheritance should be public.

Portions Copyright 2005 Microsoft Corporation. All rights reserved.

Portions Copyright Addison-Wesley Corporation. All rights reserved.

For more information on design guidelines, see the "Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries" book by Krzysztof Cwalina and Brad Abrams, published by Addison-Wesley, 2005.

See Also

Other Resources

Design Guidelines for Developing Class Libraries

Designing for Extensibility