简体   繁体   中英

C# Static method in interface cannot be accessed from the implementing class

The method SomeStaticMethod from interface IA is not directly accessible from the class A that implemented the interface. Am I missing something?

public interface IA {
    public static int SomeStaticMethod() => 4;    
}

public class A : IA {
    public static void Bidule() {
        SomeStaticMethod(); //DO NOT COMPILE
        IA.SomeStaticMethod(); //COMPILES
    }
}

If the interface IA is changed to a class, it works.

public class IA {
    public static int SomeStaticMethod() => 4;    
}

public class A : IA {
    public static void Bidule() {
        SomeStaticMethod(); //NOW it works
    }
}

A class can implement multiple interfaces. If you defined a static method with the same signature in both interfaces, the compiler couldn't decide which of those should be invoked. It's one of the (many) reasons why most OO languages try to avoid multiple-inheritance, useful as it can be.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM