views:

78

answers:

4

Here is my interface:

public interface MyInterface {
    bool Foo();
}

Here is my abstract class:

public abstract class MyAbstractClass : MyInterface {
    abstract bool MyInterface.Foo();
}

This is the compiler error: "The modifier 'abstract' is not valid for this item.

How should I go on about explicitly implementing an abstract with an abstract method?

+1  A: 

I'm not sure why you need to. Why not let the concrete implementation of the abstract class implement the member from the interface? It's the same thing really.

Phill
You mean like this?public interface MyInterface { bool Foo(); }public abstract class MyAbstractClass : MyInterface { }public class ConcreteClass : MyAbstractClass { }The problem is MyAbstractClass should implement the Foo() from MyInterface().
acermate433s
I'm not sure what you mean by "MyAbstractClass should implement the Foo() from MyInterface()". In your sample you want Foo() to be abstract so you're not implementing it. ConcreteClass will not compile if it does not provide an implementation to Foo().
Phill
+1  A: 

An abstract method has no implementation, so it can't be used to explicitly implement an interface method.

Frédéric Hamidi
Thanks for stating the obvious and at the same time obviously stating my ignorance of some C# syntax.
acermate433s
+2  A: 

You can't, basically. Not directly, anyway. You can't override a method which is explicitly implementing an interface, and you have to override an abstract method. The closest you could come would be:

bool MyInterface.Foo() {
    return FooImpl();
}

protected abstract bool FooImpl();

That still implements the interface explicitly and forces derived classes to actually provide the implementation. Are those the aspects you're trying to achieve?

Jon Skeet
Yes. I want to force classes that inherit from MyAbstractClass to implement Foo() but at the same time I want MyAbstractClass to explicitly implement MyInterface.Foo(). Right now this is what I'm doing in my code.
acermate433s
A: 

You have to use an implicit implementation of the interface member instead of an explicit implementation:

public abstract class MyAbstractClass : MyInterface
{
public abstract bool Foo();
}
Mike Dour
I see. This is a much direct to the point. Too bad I don't have any more upvotes left.
acermate433s