override a static method on a subclass in csharp

To override a static method on a subclass in C#, you can follow the below approach:

  1. Create a base class with a static method:
main.cs
public class BaseClass
{
    public static void MyStaticMethod()
    {
        Console.WriteLine("Base static method");
    }
}
128 chars
8 lines
  1. Create a derived class that inherits the base class and overrides its static method:
main.cs
public class DerivedClass : BaseClass
{
    public static void MyStaticMethod()
    {
        Console.WriteLine("Derived static method");
    }
}
146 chars
8 lines
  1. Call the static method on the derived class:
main.cs
DerivedClass.MyStaticMethod();
31 chars
2 lines

When you call the static method on the derived class, it will call the overridden static method in the derived class instead of the static method in the base class.

gistlibby LogSnag