Introduction
In C#, the new keyword provides a mechanism for hiding members of a base class in a derived class. This feature, known as method hiding or shadowing, allows developers to redefine a member of a base class in a derived class, thereby changing its behavior. This article delves into the advanced aspects of method hiding, exploring its practical applications, implications, best practices, and ways to access both hidden and hiding methods.
Understanding Method Hiding
Method hiding occurs when a derived class declares a member with the same name as a member in its base class, using the new keyword to explicitly indicate that the derived member is intended to hide the base member. This can be applied to methods, properties, events, and indexers.
Basic Example
class BaseClass
{
public void Display()
{
Console.WriteLine("Display from BaseClass");
}
}
class DerivedClass : BaseClass
{
public new void Display()
{
Console.WriteLine("Display from DerivedClass");
}
}
DerivedClass derived = new DerivedClass();
derived.Display(); // Output: Display from DerivedClass
BaseClass baseRef = derived;
baseRef.Display(); // Output: Display from BaseClass
In this example, the Display method in DerivedClass hides the Display method in BaseClass.
Practical Applications
- Versioning and Legacy Code Integration: Method hiding can be useful when integrating with legacy systems or when evolving an API. You can introduce new implementations without breaking existing code.
class LegacyComponent
{
public void ProcessData()
{
// Legacy processing logic
}
}
class UpdatedComponent : LegacyComponent
{
public new void ProcessData()
{
// Updated processing logic
}
}
- Differentiated Behavior in Derived Classes: It allows the creation of specialized behavior in derived classes while retaining the base class's implementation.
using System;
class GeneralLogger
{
public virtual void Log(string message)
{
Console.WriteLine("General log: " + message);
}
}
class SpecificLogger : GeneralLogger
{
public new void Log(string message)
{
Console.WriteLine("Specific log: " + message);
}
}
class Program
{
static void Main()
{
SpecificLogger logger = new SpecificLogger();
logger.Log("Hello"); // Output: Specific log: Hello
GeneralLogger generalLogger = logger;
generalLogger.Log("Hello"); // Output: General log: Hello
}
}
Accessing Hidden Members
When a member is hidden in a derived class, you might still need to access the hidden member from the base class. This can be achieved using base class references or casting.
Accessing Hidden Methods
class BaseClass
{
public void Show()
{
Console.WriteLine("Show from BaseClass");
}
}
class DerivedClass : BaseClass
{
public new void Show()
{
Console.WriteLine("Show from DerivedClass");
}
}
DerivedClass derived = new DerivedClass();
derived.Show(); // Output: Show from DerivedClass
// Accessing the hidden method
BaseClass baseRef = derived;
baseRef.Show(); // Output: Show from BaseClass
// Alternatively, using explicit base reference within derived class
class DerivedClass : BaseClass
{
public new void Show()
{
Console.WriteLine("Show from DerivedClass");
base.Show(); // Calling the base class method
}
}
DerivedClass derived2 = new DerivedClass();
derived2.Show(); // Output: Show from DerivedClass
// Show from BaseClass
Conclusion
Method hiding using the new keyword in C# .NET is a powerful feature that allows developers to redefine the behavior of base class members in derived classes. It is particularly useful for versioning and creating differentiated behaviors. However, it should be used sparingly and with a clear intent to avoid confusion. Understanding how to access both hidden and hiding members ensures that you can leverage this feature effectively while maintaining clean and readable code.