TechCognition

Microsoft Visual Studio 2017 Promo Code
Microsoft Office 2016 Promo Code

Shadowing in C# using new keyword

There are some situations where derived class needs to shadow the base class methods. C# provides a facility that is the logical opposite of method overriding termed shadowing. If the derived class defines a member that is same as member defined in a base class, then we call it as “the derived class has shadowed the base class.

 

Here I will explain with simple example. For example we have Base class which has display() method and Child class which also has method same name. Child class inherits Base class as shown below.

 

class Program
{
        static void Main(string[] args)
        {
            Child obj = new Child();
            obj.display();
            Console.ReadLine();
        }
    }
    class Base
    {
        public void display()
        {
            Console.WriteLine("This is from Base class");
        }
    }
    class Child :  Base
    {
        public void display()
        {
            Console.WriteLine("This is from Child class");
        }
    }

 

If you execute the above c ode you will get warning message as “’CSharpShadowing.Child.display()’ hides inherited member ‘CSharpShadowing.Base.display()’. Use the new Keyword if hiding was intended” as shown below.

 

 


To solve this issue, you have two options. You can use Override keyword for method in your child class and use virtual keyword for base class to extend the base class behavior. If you don’t have the permissions to access the base class code, you can use new keyword for your child class method to shadow the base class method as shown below.

 

class Program

{
        static void Main(string[] args)
        {
            Child obj = new Child();
            obj.display();
            Console.ReadLine();
        }
}

class Base
{
        public void display()
        {
            Console.WriteLine("This is from Base class");
        }
}
class Child :  Base
{
        public new void display()
        {
            Console.WriteLine("This is from Child class");
        }
}

 

In the above example, you are shadowing the Base class method display() in Child class by using new keyword. By using the new keyword you are able solve the warning message as we have before.

 

To discuss more about shadowing in C# or hiding in C# http://www.dotnetquestionanswers.com/index.php?topic=734.msg737#msg737

 

By using new keyword you can shadow base class variables, properties and methods. 

 

                                                                                                         CSharpShadowing.zip (20.98 kb)

Comments (6) -

  • business directory

    5/14/2012 3:55:36 PM | Reply

    Again, exceptional post. Really, I'm very happy I came across this. I'll tweet your post so my friends can read your writing too!

  • Alex K.

    6/6/2012 9:06:20 AM | Reply

    This is Wrong, you are showing how to perform hiding.
    Use Shadows keyword to shadow an element.

  • john

    6/23/2012 11:36:06 PM | Reply

    @Alex

    There is no Shadows keyword in C#, it is in Vb.Net.

    "new" keyword is equivalent of Shadows(Vb.Net Keyword) in C#

  • Ocean

    7/11/2012 9:12:04 AM | Reply

    Thanks..simply the best

  • cheap men suits

    8/17/2012 11:56:42 PM | Reply

    Thats wonderful indeed! I will definitely share this with others. Your contributions shows your expertise over the matter http://www.celebritysuits.com/

Add comment

Loading

© Copyright 2018 www.techcognition.com | Sitemap