Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The new modifier in C#, when applied to a method declaration, explicitly hides a member inherited from a base class. This mechanism, known as method hiding or shadowing, instructs the compiler to resolve the method name to the derived class’s implementation rather than the base class’s implementation. It is the syntactically correct way to suppress compiler warning CS0108, which occurs when a derived class member shares a name with a base class member without explicitly stating the intent to hide it. Unlike the override modifier, which extends an existing polymorphic chain, the new modifier severs the connection to the base class’s implementation for that specific identifier. However, the new modifier only dictates name resolution (scope and visibility); it does not dictate the dispatch mechanism. The method’s binding—whether early (static dispatch) or late (dynamic dispatch)—depends entirely on the presence of the virtual keyword in either the base or the new method declaration.

Syntax and Implementation

using System;

public class BaseClass
{
    public void Execute()
    {
        Console.WriteLine("Base implementation");
    }
}

public class DerivedClass : BaseClass
{
    // The 'new' modifier explicitly hides BaseClass.Execute()
    public new void Execute()
    {
        Console.WriteLine("Derived implementation");
    }
}

Resolution Behavior

Because method hiding relies on the declared reference type for name resolution at compile-time, casting a derived object to its base type will expose the hidden base method.
using System;

public class Program
{
    public static void Main()
    {
        // Reference type is DerivedClass, Instance type is DerivedClass
        DerivedClass derivedRef = new DerivedClass();
        derivedRef.Execute(); 
        // Output: "Derived implementation"

        // Reference type is BaseClass, Instance type is DerivedClass
        BaseClass baseRef = new DerivedClass();
        baseRef.Execute(); 
        // Output: "Base implementation"
    }
}

Polymorphic Chain Separation (new virtual)

A critical architectural feature of the new modifier is its ability to be combined with the virtual keyword. Declaring a method as new virtual explicitly hides the base member while simultaneously allocating a new virtual method table (vtable) slot. This initiates a completely new polymorphic chain for subsequent derived classes, independent of the base class’s vtable.
using System;

public class BaseClass
{
    public virtual void Process() => Console.WriteLine("Base Process");
}

public class DerivedClass : BaseClass
{
    // Hides BaseClass.Process() and starts a new polymorphic chain
    public new virtual void Process() => Console.WriteLine("Derived Process");
}

public class SubDerivedClass : DerivedClass
{
    // Overrides the NEW virtual method in DerivedClass, not BaseClass
    public override void Process() => Console.WriteLine("SubDerived Process");
}

Technical Characteristics

  • Name Resolution vs. Dispatch: The new keyword controls identifier resolution, not dispatch. If a hidden base method was declared as virtual, invoking it via a base reference still utilizes dynamic dispatch and consults the original vtable.
  • No Base virtual Requirement: The base class method does not need to be marked as virtual, abstract, or override for a derived class to hide it using new.
  • Signature Matching: To successfully hide a base method, the derived method must have the exact same signature (name and parameters). If the signatures differ, the compiler treats it as standard method overloading rather than method hiding.
  • Base Access: The hidden base method remains accessible from within the derived class by using the base keyword (e.g., base.Execute()).
Master C# with Deep Grasping Methodology!Learn More