C# Virtual Keyword

In c#, the virtual keyword is useful to override base class members such as properties, methods, etc., in the derived class to modify it based on our requirements.

 

Following is a simple example of defining a method with the virtual keyword in the c# programming language.

 

public class Users
{
   public virtual void GetInfo()
   {
      Console.WriteLine("Base Class");
   }
}

If you observe the above code snippet, we defined a GetInfo method with the virtual keyword. The GetInfo method can be overridden by any derived class that inherits properties from a base class called Users.

 

Generally, whenever the virtual method is invoked, the run-time object will check for an overriding member in the derived class. If no derived class has overridden the member, then the virtual method will be treated as an original member.

 

In c#, by default all the methods are non-virtual, and we cannot override non-virtual methods. If you want to override a method, you need to define it with the virtual keyword.

 

The virtual keyword in c# cannot be used with static, abstract, private, or override modifiers. In c#, the virtual inherited properties can be overridden in a derived class by including a property declaration that uses the override modifier.

C# Virtual Keyword Example

Following is the example of using a virtual keyword to allow class members to be overridden in a derived class in the c# programming language.

 

using System;

namespace Tutlane
{
    // Base Class
    public class BClass
    {
       public virtual string Name { get; set; }
       public virtual void GetInfo()
       {
          Console.WriteLine("Learn C# Tutorial");
       }
    }
    // Derived Class
    public class DClass : BClass
    {
       private string name;
       public override string Name
       {
          get
          {
             return name;
          }
          set
          {
             if (!string.IsNullOrEmpty(value))
             {
                name = value;
             }
             else
             {
                name = "No Value";
             }
          }
       }
       public override void GetInfo()
       {
          Console.WriteLine("Welcome to Tutlane");
       }
    }
    class Program
    {
       static void Main(string[] args)
       {
          DClass d = new DClass();
          d.GetInfo();
          BClass b = new BClass();
          b.GetInfo();
          d.Name = string.Empty;
          Console.WriteLine(d.Name);
          Console.WriteLine("\nPress Enter Key to Exit..");
          Console.ReadLine();
       }
    }
}

If you observe the above example, we are overriding a base class (BClassmethods and properties which we defined with a virtual keyword in a derived class (DClass) using the override keyword.

 

When you execute the above c# program, you will get the result below.

 

C# Virtual Keyword Example Result

 

This is how we can use virtual keyword in c# to allow class members, such as methodsproperties, etc., to be overridden in a derived class based on our requirements.