Author avatar

Dániel Szabó

Calling Virtual and Non-virtual Methods for C#

Dániel Szabó

  • Nov 7, 2019
  • 8 Min read
  • Nov 7, 2019
  • 8 Min read
Languages Frameworks and Tools


The third pillar of object-oriented programming is commonly called polymorphism, a Greek word that is translated as "many shaped."

Polymorphism has two distinct aspects. At runtime, objects of a derived class can be treated as objects of a base class in specific places such as method parameters and collections or arrays. When this occurs, the declared type of the object is no longer identical to its run-time type. The other aspect is that base classes can define and implement virtual methods, and the derived classes are able to override them. The derived classes inject their own implementation and definition of the virtual method. This means that in your application, you are able to call a method on a base class and cause its derived class's method to be executed. This is done behind the scenes with the magic of CLR, which looks up the run-time type of the object and invokes the overridden version of the virtual method.

Virtual and Non-virtual Methods

Virtual and non-virtual methods support the polymorphistic features of C#, combining the virtual keyword with the override. With the combination of the virtual on the base class method and the override on the method in the derived class, both methods are said to be virtual methods.

In simple terms, this method can be redefined in derived classes.

The implementation is present in both the base and derived class, and most of the time in derived classes, extra functionality is added. When we declare a method as virtual in a base class, then we have the possibility to define it in the base class, and optionally the override becomes available in derived classes. When the method is declared as virtual in a base class, and the same definition exists in a derived class, there is no need for override, but a different definition will only work if the method is overridden in the derived class.

Two important rules:

  1. By default, methods are non-virtual, and they cannot be overridden.
  2. Virtual modifiers cannot be used with static, abstract, private, and override modifiers.

Let's demonstrate this with a code example.

1using System;
3namespace virtnonvirt
5    public class Person
6    {
7        public string _name;
8        public int _age;
9        public string _sex;
10        public Person(string name, int age, string sex)
11        {
12            this._age = age;
13            this._sex = sex;
14            this._name = name;
15        }
17        public void Age() {
18            Console.WriteLine($"Person :: Hi, I am {this._age} years old!");
19        }
20        public virtual void Introduction()
21        {
22            Console.WriteLine($"Person :: Hi, my name is {this._name}, I am {this._age} years old and I am a {this._sex}");
23        }
24    }
26    public class Men : Person
27    {
28        public Men(string name, int age) : base(name, age, "male")
29        {
30        }
31        public  void  Age()
32        {
33            Console.WriteLine($"Men :: Hi, I am {this._age} years old!");
34        }
35        public override void Introduction()
36        {
37            Console.WriteLine($"Men :: Hi, my name is {this._name}, I am {this._age} years old and I am a {this._sex}");
38        }
39    }
40    class Program
41    {
42        static void Main(string[] args)
43            {
44            Men Dani = new Men("Szabó Dániel Ernő", 28);
45            Person Alien = Dani;
46            Dani.Introduction();
47            Alien.Introduction();
48            Dani.Age();
49            Alien.Age();
50            Console.ReadKey();
51            }
52    }

The output of the code looks something like this:

1Men :: Hi, my name is Szabó Dániel Erno, I am 28 years old and I am a male
2Men :: Hi, my name is Szabó Dániel Erno, I am 28 years old and I am a male
3Men :: Hi, I am 28 years old!
4Person :: Hi, I am 28 years old!

But what is happening behind the scenes? Let's dissect the example.

We have two classes. One is Person and the other is Men. The Person is the base class with two methods, Age() and Introduction(). The latter is a virtual method, the former is a non-virtual method. The Introduction() method is overridden in the derived class.

In our Main function, we instantiate the Men class with the instance Dani and then assign the instance to the Person base classes new instance. When the virtual and non-virtual methods are invoked in both of the class instances, the subclass override is invoked in both cases, showcasing the polymorphism and the internal working of the virtual keyword.

How the Override Works

Here's another great example to demonstrate how this override works.

1using System;
3namespace virtnonvirt
5    public class ConfigurationItem {
6        public string _location;
7        public string _name;
8        public ConfigurationItem(string name, string location)
9        {
10            this._name = name;
11            this._location = location;
12        }
13        public ConfigurationItem(string name)
14        {
15            this._name = name;
16            this._location = "Default Location";
17        }
19        public virtual void DeviceInfo()
20        {
21            Console.WriteLine($"ConfigurationItem :: The device with name: {this._name} is located in datacenter: {this._location}");
22        }
23    }
25    public class Server : ConfigurationItem
26    {
27        public Server(string name, string location) : base(name, "DCA")
28        {
30        }
31        public override void DeviceInfo()
32        {
33            Console.WriteLine($"Server :: The device with name: {this._name} is located in datacenter: {this._location}");
34        }
36    }
37    public class Switch : ConfigurationItem
38    {
39        public Switch(string name) : base(name) { }
40    }
41    class Program
42    {
43        static void Main(string[] args)
44            {
45            Server DomainController = new Server("CUSTOMERDC1", "LasVegas");
46            DomainController.DeviceInfo();
47            Switch L2Switch = new Switch("CUSTOMERSW1");
48            L2Switch.DeviceInfo();
49            Console.ReadKey();
50            }
51    }

The output of the execution looks like this:

1Server :: The device with name: CUSTOMERDC1 is located in datacenter: DCA
2ConfigurationItem :: The device with name: CUSTOMERSW1 is located in datacenter: Default Location

Let's dissect this example and see what is happening behind the scenes.

This time we have three classes: ConfigurationItem, Server, and Switch. The first one is the base class. Both Server and Switch are derived from the base class. Base class ConfigurationItem has a virtual method called DeviceInfo(). This method is overridden in the Server class but not in the Switch class. When the virtual methods are overridden in a derived class, that derived class uses an instance, then invokes a derived class overridden method. When the virtual method is not overridden in a derived class and uses that derived class instance, it invokes the base class's virtual method.


In this written guide we have explored the functions that allow you to create polymorphic code in C# using virtual and non-virtual methods. This is a pretty neat tool in a developer's toolset. It allows you to create more robust applications that are easy to extend and stand the test of time. We have walked through two distinct examples that give you an idea as to how you can implement this feature and incorporate it into your application.

I hope this has been informative and worth your time. Thanks for reading, and give it a thumbs up if you liked it.