C# Override Keyword

In c#, override keyword is used to override a base class virtual 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 override keyword in c# programming language.

 

// Base Class

public class Users

{

    public virtual void GetInfo()

    {

        Console.WriteLine("Base Class");

    }

}

// Derived Class

public class Details : Users

{

    public override void GetInfo()

    {

        Console.WriteLine("Derived Class");

    }

}

If you observe above code snippet, we defined a GetInfo method with virtual keyword in base class (Users) so the GetInfo method can be overridden by any derived class that inheritsproperties from base class using override keyword.

 

In c#, we cannot override a non-virtual or static methods. In case, if you want to override a method, then you need to define it with virtual keyword.

 

To override a base class method in derived class, then both override method and virtual method must have a same signatures and access modifiers.

 

In c#, we should not use static, new, or virtual modifiers to modify an override method. The overriding property declaration must specify exactly the same access modifier, type and name as inherited property.

C# Override Keyword Example

Following is the example of using an override keyword to override a virtual members of base 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# Override Keyword Example Result

 

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

 
 

NEXT

C# Base Keyword