static modifier and interfaces

A

Andy

Why can't I create an interface and make its implementation static/shared?

Is there some pattern that lets me work around this?

Any explanation of why this is would be appreciated.



This is the message I get for the following code:

'staticInterface.Class1' does not implement interface member
'staticInterface.foo.doSomething(int)'.
'staticInterface.Class1.doSomething(int)' is either static, not public, or
has the wrong return type.


interface foo

{

bool doSomething(int y);

}


public class Class1 : foo

{

public bool x() { }

static public bool doSomething(int y)

{

// TODO: Add Class1.doSomething implementation

return false;

}


}
 
M

Marina

The interface you wrote, is a contract. Meaning any class implementing it
has to have doSomething as a public instance method. That way if you have
an instance of the class that implements your interface, you know you can
call doSomething on it.

However, if you had the option of making doSomething static - how can you be
sure that you can call this method on an instance? You can't - you would
never know if it was an instance or static implementation.
 
1

100

Hi Andy,
Interface members are implicitly abstract (virtual) and have to be
implemented.
You can think of implementing an interface members as overriding them.
Overriding make sense for instance methods only.

HTH
 
A

Andy

Ok, interfaces are contract - yeah. But why limit them to instance methods?

Doesn't it make sense that you might sometime want to create a group of classes that implement specific behavior (contract) that is static?

Is there a pattern that will let me work around this?
 
J

Jon Skeet [C# MVP]

Andy said:
Ok, interfaces are contract - yeah. But why limit them to instance methods?

Doesn't it make sense that you might sometime want to create a group of
classes that implement specific behavior (contract) that is static?

It does, but just being able to specify a static method in the
interface is only half the story - the other half is invoking that
method. How would you expect to do that?
Is there a pattern that will let me work around this?

It depends on exactly what you're doing. The factory pattern might be
helpful to you.
 
G

Girish Bharadwaj

Andy said:
Ok, interfaces are contract - yeah. But why limit them to instance methods?

Doesn't it make sense that you might sometime want to create a group of
classes that implement specific behavior (contract) that is static?

*Is there a pattern that will let me work around this?*


Here is what I think (1 1/2 c)..
*static* methods are a contract from the "Type" but not from an
"instance". i.e. The mere existence of a particular Type would guarantee
the existence of that *static*. Now, Interface are instance specific.
This has to be the case since interfaces are meant to discover and use
abilities of similar instances of types.

With that in mind, it would not really make sense to declare a interface
method as static since that would mean that the contract being agreed
upon is actually from the *type* of the derived class and not by the
*instance*.

And of course, the real thing to note is that if you consider COM
interfaces (or any implementation of interfaces) they are just runtime
vtbl pointers to concrete instance method, that would just not work with
a static.

Anyway, if you really want a static method to be part of a contract, you
can do that using a simple facade pattern to wrap the underlying
interfaces and use Template method + Factory to generate and use the
real interface implementations.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top