Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Internal Modifier on an Interface's Property

I have the following interface:

public interface IAgable {
    int Age { get; internal set; }
}

I want the Age property, to be read-only for external assemblies that reference this interface, but also I want the Age property to be set on the interface's same assembly, hence the internal modifier.

This, however, seems to throw a compilation error, as accessibility modifiers may not be used on accessors in an interface.

I want the property to be called from an interface, and I want to be able to set it at an internal level. At the same time, if referenced from an outside project, I want it to be readonly.

Is this possible?

like image 499
Matias Cicero Avatar asked Jan 28 '26 16:01

Matias Cicero


1 Answers

Have an internal interface that provides both a get and a set, and a public interface that provides only a get. Have the public interface extend the internal interface:

public interface IAgable
{
    int Age { get; }
}

internal interface IAgableInternal : IAgable
{
    int Age { set; }
}
like image 51
Servy Avatar answered Jan 31 '26 05:01

Servy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!