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//  ww w . j a  va2s.  com
{
    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