I tried to inherit interface, and make some of the automatically generated set property as private. This is an example.
public class MyClass
{
public interface A
{
int X {get; set;}
}
public interface B : A
{
int Y {get; set;}
}
public class C : A
{
public int X {get; private set;}
}
When I tried to compile it. I got an error 'MyClass.C' does not implement interface member 'MyClass.A.X.set'. 'MyClass.C.X.set' is not public.
.
I tried with private set;
in iterface A
, but I got this error again : 'MyClass.A.X.set': accessibility modifiers may not be used on accessors in an interface
.
Is this accessibility modifier not allowed in C#?
I tried with private set; in iterface A, but I got this error again
If your interface only requires that a property should be retrievable, you define it as:
public interface A
{
int X {get;} // Leave off set entirely
}
The declaration of an interface defines the public set of members that the implementing type must have. So, if C
implements A
, it must have a public member for each member defined by the interface.
A
defines that any implementing type must have a public property X
with a public getter and a public setter. C
does not meet this requirement.
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