0
0
CsharpConceptBeginner · 3 min read

C# new Keyword for Hiding: What It Is and How to Use It

In C#, the new keyword is used to hide a member inherited from a base class by declaring a new member with the same name in the derived class. This tells the compiler to use the derived class's member instead of the base class's member when accessed through the derived class type.
⚙️

How It Works

Imagine you have a family recipe book (base class) with a recipe called "Chocolate Cake." Now, you want to create your own version of the recipe in your personal cookbook (derived class) but keep the same name. Using the new keyword in C# is like saying, "I have my own Chocolate Cake recipe that hides the original one."

Technically, when you use new to declare a member in a derived class with the same name as one in the base class, you hide the base class member. This means if you access the member through the derived class, you get the new version. But if you access it through a base class reference, you still get the original.

This is different from overriding, which replaces the base member's behavior even when accessed through a base class reference. Hiding is more like putting a new sign over the old one, visible only when you look at the new sign directly.

💻

Example

This example shows a base class with a method, and a derived class that hides it using new. Notice how the output changes depending on the reference type.

csharp
using System;

class Animal
{
    public void Speak()
    {
        Console.WriteLine("Animal speaks");
    }
}

class Dog : Animal
{
    public new void Speak()
    {
        Console.WriteLine("Dog barks");
    }
}

class Program
{
    static void Main()
    {
        Animal myAnimal = new Animal();
        myAnimal.Speak();  // Calls Animal's Speak

        Dog myDog = new Dog();
        myDog.Speak();     // Calls Dog's Speak (hides Animal's)

        Animal animalDog = new Dog();
        animalDog.Speak(); // Calls Animal's Speak because of hiding
    }
}
Output
Animal speaks Dog barks Animal speaks
🎯

When to Use

Use the new keyword for hiding when you want to provide a new implementation of a member in a derived class but do not want to override the base class member's behavior. This is useful when the base class member is not marked as virtual or when you want to keep both versions accessible depending on the reference type.

For example, if you have a legacy base class you cannot change, and you want to add a new version of a method in your derived class without affecting existing base class behavior, hiding with new is appropriate.

Remember, hiding can cause confusion if not used carefully, so prefer overriding when polymorphic behavior is desired.

Key Points

  • The new keyword hides a base class member with a new member in the derived class.
  • Hiding affects which member is called based on the reference type, not the actual object type.
  • It is different from overriding, which requires the base member to be virtual and supports polymorphism.
  • Use hiding when you cannot or do not want to override the base member.
  • Be cautious as hiding can lead to unexpected behavior if misunderstood.

Key Takeaways

The C# new keyword hides a base class member by declaring a new member with the same name in the derived class.
Hiding changes which member is called based on the reference type, unlike overriding which is polymorphic.
Use new when the base member is not virtual or you want to keep both base and derived versions accessible.
Hiding can cause confusion, so prefer overriding when polymorphism is needed.
Always explicitly use new to avoid compiler warnings about hiding members.