is there a way to initialize an eii property inside a constructor?

I think the only other possibility besides your suggested approach of using a backing field rather than an auto-property is to use the property initializer syntax, e.g.:

int ILeft.P { get; } = 0;

Of course with this you can’t initialize based on, say, an argument passed to the constructor, so it provides only limited additional flexibility.

(There is a proposal to add “primary constructors” to the C# language, which might make it possible to initialize a get-only explicitly implemented interface property based on a constructor argument, though as far as I know there’s no timeline for its inclusion in the language so I wouldn’t hold your breath…)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top