C# Override Keyword

In c#, the override keyword is used to override a base class virtual members such as properties, methods, etc., in the 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 the above code snippet, we defined a GetInfo method with a virtual keyword in the base class (Users). The GetInfo method can be overridden by any derived class that inherits properties from the base class using the override keyword.

 

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

 

To override a base class method in the derived class, both the override and virtual methods must have the 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 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 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 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 the virtual keyword in a derived class (DClass) using override keyword.

 

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

 

C# Override Keyword Example Result

 

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