What is Shadowing in .NET?

Shadowing in .NET

When a method is defined in base class are not overridable and one needs to provide different implementation for the same in derived class. In such scenario one can hide the base class implementation and provide new implementation using Shadows (VB.Net)/new(C#) keyword.

Below code shows use of new keyword in C#

static void Main(string[] args)
        {
            new Employee().Display();
            new EmployeeNew().Display();
            Employee a = new EmployeeNew();
            a.Display();
            Console.Read();
        }

        public class Employee
        {
            public void Display()
            {
                Console.WriteLine(“Display Parent class”);
            }
           

        }
        partial class EmployeeNew : Employee
        {
            public new void Display()
            {
                Console.WriteLine(“Display new Child class”);
            }
          

        }

OUTPUT :

Display Parent class
Display new Child class
Display Parent class

One thought on “What is Shadowing in .NET?

Leave a Reply