Method Overriding in C#

In this article I will explain the concept of Method overriding in C#.
  • 2620

Introduction

The concept of method overriding is very simple in C#. The override modifier is use to modify a method, a property, an indexer, or an event. When we want to Create a method in derived class with same signature as a method in base class then the concept of method overriding is used. Method overriding is possible only in derived classes, but not within the same class.

Example

using System;

using System.Collections.Generic;

using System.Text;

namespace methodoverriding

{

    class BaseClass

    {

        public virtual string Myhom()

        {

            return "Delhi";

        }

    }

    class DerivedClass : BaseClass

    {

        public override string Myhom()

        {

            return "Noida";

        }

    }

 

    class Program

    {

    static void Main(string[] args)

        {

            DerivedClass obj = new DerivedClass();

            string city = obj.Myhom();

            Console.WriteLine(city);

            Console.Read();

        }

    }

}


The output of the following program

Clipboard170.jpg

© 2020 DotNetHeaven. All rights reserved.