What is method hiding in C#?

CsharpProgrammingServer Side Programming

Method hiding is also known as shadowing. The method of the parent class is available to the child class without using the override keyword in shadowing. The child class has its own version of the same function.

Use the new keyword to perform shadowing.

Let us see an example.

Example

 Live Demo

using System;
using System.Collections.Generic;

class Demo {
   public class Parent {
      public string GetInfo () {
         return "This is Parent Class!";
      }
   }

   public class Child : Parent {
      public new string GetInfo() {
         return "This is Child Class!";
      }
   }

   static void Main(String[] args) {
      Child child = new Child();
      Console.WriteLine(child. GetInfo());
   }
}

Output

This is Child Class!
raja
Published on 16-Aug-2018 12:26:02
Advertisements