C# Virtual Keyword

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

 

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

 

public class Users

{

    public virtual void GetInfo()

    {

        Console.WriteLine("Base Class");

    }

}

If you observe above code snippet, we defined a GetInfo method with virtual keyword so the GetInfo method can be overridden by any derived class that inheritsproperties from 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. In case, if no derived class has overridden the member, then the virtual method will be treated it as original member.

 

In c#, by default all the methods are non-virtual and we cannot override a non-virtual methods. In case, if you want to override a method, then you need to define it with 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 a class members to be overridden in a derived class in 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 Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

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

 

When we execute above c# program, we will get the result like as shown below.

 

C# Virtual Keyword Example Result

 

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