Override a Static Method in C#

In this article, we will discuss overriding the static method of an abstract base class.
Override a Static Method in C#
First, we cannot override a static method that belongs to an abstract base class. That is not something that can be done in C#.
This is because the phrase static refers to something that applies to everyone, while the overriding idea is what we utilize to change the implementation according to our requirements.
When we override a static method, we lose the static property that comes with it. Consequently, static methods in C# cannot be overridden.
However, we can break the rules and do the task in the opposite order to meet the requirements.
Alternate Way to Override a Static Method in C#
Hiding is an idea that may be used, and it is possible to use. This idea is also significant to the Early Binding and Late Binding used when building virtual functions.
When virtual method implementations are chosen at runtime based on the instance, then this is called overriding. When more than one method with the same name is chosen at build time, it is known as overloading.
Overloading is more of a convenience than an essential component of object-oriented polymorphism.
Overloading static methods may first resemble overriding. The compiler searches up in the inheritance structure until it finds a matching method if it cannot locate a static method in the supplied class.
The method is resolved and fixed at build time, which is a key distinction. Because this is not true overriding, you cannot label static methods as virtual
, override
, or new
.
The behavior of an overriding method should respect the Liskov Substitution Principle (LSP). More generally speaking, the behavior of an overriding method should adhere to the contract of the virtual method.
These constraints do not apply to overloaded static methods, which are more accurately conceived of as distinct methods with the same name but serving different functions.
The new
keyword establishes members with the same name but different actions.
When the new
keyword is used, this warning will not appear as it usually would. In addition, bear in mind that you don’t even have to utilize delegates to call the parents’ hidden methods anytime you want; all you have to do is give them their full names.
Implementation of Hiding a Static Method in C#
using System;
class Hi
{
static void Main() {
C2.Calldisplay();
}
}
class C1
{
public static void display() {
System.Console.WriteLine("Class 1 it is");
}
}
class C2 : C1
{
public static void display() {
System.Console.WriteLine("Class 2 it is");
}
public static void Calldisplay()
{
display();
C1.display();
}
}
Output:
Class 2 it is
Class 1 it is
The display()
method is inherited from the C1
class by the C2
class, which will call C1.display()
. The function named C1.display()
in the C2
class, inherited from the C1
class, will call the hidden static method on the parent, which is the C1
class.
The fact that static methods cannot be called on an instance and must be invoked by providing the name of the type to which they belong justifies the practice of hiding them, in our view. The most important decision to make about a method is whether it should be a static method, an instance
method, or a virtual
method.
The consequence of using the new
term is that the polymorphism cannot be maintained. While it might help reinterpret certain behaviors, we need to use it with great caution and be very cautious about how and where we use it.
Anyone who views the base class while invoking a redefined property will obtain the previous version of the property rather than the current one.
It is important to remember that partial classes cannot be specified across assemblies since this is a restriction imposed by the language.
In an ideal world, you should use them to partition an extensive class into many files (otherwise, you could be doing something incorrectly). You can sometimes use them to share code to facilitate cross-platform development (because not every code works on every platform).
If you are developing for several platforms, in that situation, you may be able to utilize this strategy; however, you should look into alternative ways of modeling your classes. To add insult to injury, it is utterly illegible!
When partial classes are used, it is common for there to be an extended search for all of the components. This does nothing except make it more difficult for newcomers to comprehend what is happening.
By doing things this way, we can complete the work of overriding a static method of an abstract base class using another way.
I'm a Flutter application developer with 1 year of professional experience in the field. I've created applications for both, android and iOS using AWS and Firebase, as the backend. I've written articles relating to the theoretical and problem-solving aspects of C, C++, and C#. I'm currently enrolled in an undergraduate program for Information Technology.
LinkedInRelated Article - Csharp Method
- Optional Parameter in C#
- Pass a Method as a Parameter in C# Function
- Sort a List by a Property in the Object in C#
- C# Pass Multiple Parameters to a Get Method
- Method Groups in C#