Visibility and Accessibility Modifiers
Virtual Modifier
The virtual modifier is also used in inheritance scenarios, and it has a specific purpose. It allows you to mark type members to provide a default implementation, which can be optionally overriden in your subclasses.
using System; public class Animal ...
The virtual modifier can only be applied to type members, it cannot be applied to classes. In the above example, we've marked the MakeSound method as virtual. This means we can provide our default implementation:
Console.WriteLine("Hello");
This is then overriden (again using the override modifier) and provided a custom implementation when we create our Dinosaur class:
Console.WriteLine("Roar");
At runtime, when your code is executing a virtual method, the runtime type of the object is used to find the override of the method if it exists to execute it, otherwise it falls back to the default implementation.