omega
omega

Reputation: 43833

How to access properties from parent class when implementing interface in c#?

If I have an interface

interface ISite {
    int g { get; set; }
}

and a base class

class Site {
    public int g = 1;
}

and a derived class that implements the interface and extends the class

class DSite : Site, ISite {
    // comaplains g is not defined
}

It complains that I did not implement the g property, but how do I make it take it from the parent class Site?

Upvotes: 0

Views: 307

Answers (1)

Andrei Tătar
Andrei Tătar

Reputation: 8295

Either make g in Site a property:

class Site
{
    public int g {get;set;} = 1;
} 

or explicitly implement the interface in DSite:

class DSite : Site, ISite
{
    int ISite.g
    {
        get { return g; }
        set { g = value; }
    }
}

Upvotes: 1

Related Questions