interface IXXX
{
void Foo();
}
class XXX : IXXX
{
public static void Foo()
{
Console.WriteLine("From XXX");
}
}
class Program
{
static void Main(string[] args)
{
XXX.Foo();
}
}
Compiler error: XXX.Foo() cannot implement an interface member because it is static.
Why can't a static method implement an interface method?
See this thread from JoelOnSoftware describing the reasons behind this.
Basically the interface is the contract between the consumer and the provider, and a static method belongs to the class, and not each instance of the class as such.
An earlier question on SO also deal with the exact same question: Why Doesn't C# Allow Static Methods to Implement an Interface?
An interface defines the behaviour that an object must respond to. As Foo is a static method, the object doesn't respond to it. In other words, you couldn't write...
XXX myXXX = new XXX();
myXXX.Foo();
In other words, myXXX doesn't fully satisfy the requirements of the interface.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With