CSharp - Hide parent method and call it via base keyword

Description

Hide parent method and call it via base keyword

Demo

using System;


class Parent/*w  ww.ja va 2 s.  c  o  m*/
{
    public void ShowMe()
    {
        Console.WriteLine("I am inside the Parent method");
    }
}
class Child : Parent
{

    public void ShowMe()
    {
        Console.WriteLine("I am inside the Child method");
        //base.ParentMethod();
    }
}

class Program
{
    static void Main(string[] args)
    {
        Child obChild = new Child();
        obChild.ShowMe();
    }
}

Result

The program was compiled and run.

You are receiving a warning message saying that your derived class method hides the inherited parent class method.

The base keyword can be used in either of the following scenarios:

  • To call a hidden/overridden method defined in a parent class.
  • To call particular base-class constructor.

Both static and non-static constructors and destructors are not inherited.

Related Topic