Home
C#
virtual Keyword
This page was last reviewed on Jun 5, 2021.
Dot Net Perls
Virtual. Consider the runtime types of things in a C# program. With a virtual call, an object's most derived type is used for a method invocation.
A virtual method can be redefined. The virtual keyword designates a method that is overridden in derived classes. We can add derived types without modifying the rest of the program.
An example. Class A has a public virtual method called Test. Class B, meanwhile, derives from class A and it provides a public override method called Test as well.
Tip The virtual modifier tells the compiler that when any class derived from class A is used, an override method should be called.
override
Info With virtual methods, the runtime type is always used to determine the best method implementation.
Detail Ref1 has a compile-time and runtime type of A. On the other hand, ref2 has a compile-time type of A but a runtime type of B.
using System; class A { public virtual void Test() { Console.WriteLine("A.Test"); } } class B : A { public override void Test() { Console.WriteLine("B.Test"); } } class Program { static void Main() { // Compile-time type is A. // Runtime type is A as well. A ref1 = new A(); ref1.Test(); // Compile-time type is A. // Runtime type is B. A ref2 = new B(); ref2.Test(); } }
A.Test B.Test
Private virtual. Private virtual methods cannot be declared in the C# language. For private methods, you have already entered the data object.
Thus No virtualized entry point is ever required. And a private virtual method is not useful.
Here We see both a program that attempts to use a private virtual method member, as well as the compile-time error.
Note The error occurs during the static compilation phase. No program that declares a private virtual method will ever be executed.
class A { private virtual int Test() { return 1; } } class Program { static void Main() { } }
Error 1 'A.Test()': virtual or abstract members cannot be private
A discussion. Why would you need to use virtual methods? Your program may be designed in such a way that you do not know all the types of objects that will occur when it is executed.
And You can provide a standard (base) type and design around that type. Then add methods for the more specific (derived) types.
Detail When you call a method on the base type, you invoke the more derived (and useful) method.
Abstract. The term abstract refers to a conceptual type. It has no implementation. An abstract class then is used as a template for derived classes.
abstract
Notes, polymorphism. Virtual methods are an implementation of type-based polymorphism. This gives you a dynamic entry point to the class type.
Note This enables you to clearly separate the usage of the type from the implementation of each subtype.
Info If you are already inside the type, you can simply call a regular instance private method.
Notes, performance. Instance and static methods are faster to invoke than interface and virtual methods. Speed is important in many programs.
static
A summary. Virtual specifies that the method can be overridden in derived types. It tells the compiler to generate code that selects an override method.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jun 5, 2021 (edit).
Home
Changes
© 2007-2024 Sam Allen.