Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call static method for a generic type in a class?

Tags:

c#

interface TerminationCondition
{
    static bool IsSatisfied(some parameters);
}

Let's say some classes A and B implement this interface.

Some other class C has a generic type:

class C<termCondition> where termCondition : TerminationCondition

I need to call IsSatisfied from C accordingly, whether I have passed A or B for the generic type. For some reason it is not possible termCondition.IsSatisfied().

To summarize, the question is How do I call static method for a generic type in a class?

like image 969
Miyamoto Musashi Avatar asked Feb 22 '26 04:02

Miyamoto Musashi


2 Answers

This is not possible.

In fact, you can't have static methods in an interface at all.

like image 114
SLaks Avatar answered Feb 23 '26 16:02

SLaks


Since termCondition is required to be of type TerminationCondition, you could simple have IsSatisfied be an instance method of the classes that implement that interface. There's no need to have that method be static at all.

like image 38
Austin Salonen Avatar answered Feb 23 '26 17:02

Austin Salonen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!