Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I force inheriting classes to implement a static method in C#?

Tags:

All I want to do is make sure that child classes of the class Item implement a static method and I want this to be checked at compile time to avoid runtime errors.

abstract classes with static methods don't seem to work:

ERROR: A static member cannot be marked as override, virtual, or abstract

public abstract class Item
{
    public static abstract Item GetHistoricalItem(int id, DateTime pastDateTime);
}

public class Customer : Item
{
    public static override Customer GetHistoricalItem(int id, DateTime pastDateTime)
    {
        return new Customer();
    }
}

public class Address : Item
{
    public static override Address GetHistoricalItem(int id, DateTime pastDateTime)
    {
        return new Address();
    }
}

and interfaces don't seem to work either:

ERROR: Customer does not implement interface member GetHistoricalItem()

public class Customer : Item, HistoricalItem
{
    public static Customer GetHistoricalItem(int id, DateTime pastDateTime)
    {
        return new Customer();
    }
}

public class Address : Item, HistoricalItem
{
    public static Address GetHistoricalItem(int id, DateTime pastDateTime)
    {
        return new Address();
    }
}

interface HistoricalItem
{
    Item GetHistoricalItem();
}

Is there some workaround for this to have the compiler check if inheriting classes implement a static method with a certain signature or not?

like image 883
Edward Tanguay Avatar asked Dec 15 '09 08:12

Edward Tanguay


People also ask

How do you call a static method in inherited class?

A static method is the one which you can call without instantiating the class. If you want to call a static method of the superclass, you can call it directly using the class name.

Can a class inherit a static method?

Static method can be inherited similar to normal methods, however unlike normal methods it is impossible to create "abstract" methods in order to force static method overriding.

Can static function be inherited in C?

Quick A: Yes, and there are no ambiguity with static members.

Will static methods be inherited can they be overridden?

Static methods in Java are inherited, but can not be overridden. If you declare the same method in a subclass, you hide the superclass method instead of overriding it. Static methods are not polymorphic. At the compile time, the static method will be statically linked.


2 Answers

There is a workaround i figured out for your scenario:

public class Customer : Reference<Customer>, IHistoricalItem
{
}

public class Address : Reference<Address>, IHistoricalItem
{
}

public interface IHistoricalItem
{
}

public class Reference<T> where T : IHistoricalItem, new()
{
    public static T GetHistoricItem(int id, DateTime pastDateTime)
    {
        return new T();
    }
}

Hope this helps!!

like image 119
viky Avatar answered Oct 04 '22 20:10

viky


This cannout be done.

Have a look at Why can’t I have abstract static methods in c#?

like image 29
Adriaan Stander Avatar answered Oct 04 '22 22:10

Adriaan Stander