© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
8 replies
CyberBotX

❔ How to call static virtual interface method without completely reimplementing in derived class

This mainly pertains to generic math in C# 11 / .NET 7, but say I have a minimum of something like this:
interface ITest
{
    static virtual void Test() => Console.WriteLine("Test");
}
class Testing : ITest
{
}
interface ITest
{
    static virtual void Test() => Console.WriteLine("Test");
}
class Testing : ITest
{
}

I cannot find a way to call the
Test
Test
method from
ITest
ITest
without just redefining it entirely in
Testing
Testing
, even if said redefining is 100% identical to the interface's code (which seems terrible to me). Obviously I can't call
ITest.Test()
ITest.Test()
because it is an interface and not a class, and I can't call
Testing.Test()
Testing.Test()
because the method technically doesn't exist on
Testing
Testing
. The only workaround I've found is doing something like
void RunTest<T>() where T : ITest => T.Test();
void RunTest<T>() where T : ITest => T.Test();
and calling it via
RunTest<Testing>();
RunTest<Testing>();
but that doesn't work for my purposes. I also can't define the method in
Testing
Testing
as
public static void Test() => ITest.Test();
public static void Test() => ITest.Test();
for the same reason as being unable to just call the last part due to it being on an interface.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

How to call a static method form interface in a generic class.
C#CC# / help
4y ago
How to override virtual interface in an interface
C#CC# / help
4y ago
interface vs extension method vs method in class
C#CC# / help
3y ago