Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Properties in Abstract Base Classes

I have a strange problem that I could not solve. When I try to compile the following snipped I get this error:

'AbstractClass' does not implement interface member 'Property' (Compiler Error CS0535)

The online help tells me to make my AbstractClass abstract, which it is. Can anybody tell me where I went wrong?

Cheers Rüdiger

public interface IBase {
    string Property { get; }
}

public abstract class AbstractClass : IBase
{
    public override string ToString()
    {
        return "I am abstract";
    }
}

public class ConcreteClass : AbstractClass
{
    string Property { 
        get {
            return "I am Concrete";
        }
    }
}
like image 819
Rüdiger Avatar asked Aug 27 '09 12:08

Rüdiger


1 Answers

Your AbstractClass needs to provide an implementation for Property from the IBase interface, even if it's just abstract itself:

public abstract class AbstractClass : IBase
{
    public override string ToString()
    {
        return "I am abstract";
    }

    public abstract string Property { get; }
}

Update: Luke is correct that the concrete implementation will need to specify that Property is an override, otherwise you'll get a "does not implement inherited abstract member" error:

public class ConcreteClass : AbstractClass
{
    public override string Property { 
        get {
            return "I am Concrete";
        }
    }
}
like image 139
dahlbyk Avatar answered Oct 11 '22 12:10

dahlbyk