I tend to favor explicit interface implementations over implicit ones, as I think programming against the interface as opposed to against an implementation, is generally preferable, plus when dealing with web-services it is often a necessity.
That said, I was wondering why the following is illegal with an explicit interface declaration and legal with an implicit one:
interface IConnection
{
string ConnectionString { get; }
}
class Connection1 : IConnection
{
// private set is illegal, won't compile
string IConnection.ConnectionString { get; private set; }
}
class Connection2 : IConnection
{
// private set is legal now, it is not part of the interface
string ConnectionString { get; private set; }
}
I know how to fix this, as it is legal to have both an explicit and implicit interface, plus I can make the implicit interface implementation completely private.
Yet I am wondering about the reasoning behind this. Because technically, the internally compiled private method set_IConnection_ConnectionString
does not need to be part of the interface, right? It could just be seen as an auxiliary setter, not part of the interface, as it is in the implicit implementation situation.
Update: as a bonus, the seemingly confusing, and in my opinion not quite right compile error you receive is the following:
The accessibility modifier of the accessor must be more restrictive than the property Connection1.ConnectionString
Excuse me, more restrictive than private
, how... what?
private
keyword, but the latter being really weird and pointing the developer in the wrong direction. – Dunniteprivate
modifier as mentioned in my question. I didn't try Roslyn yet (not installed on this dev machine). Still, even "private not allowed" implies that something else is allowed, but it isn't (but I haven't come across that one using the above example). As an aside, notice that the error mentionsConnection1.ConnectionString
, which seems to refer to a non-explicitly implemented property. – Dunnite