You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Being able to declare separate readonly and writeonly interfaces that could be "joined" into a common read-write interface including properties and indexers would allow great interface designs.
publicinterfaceIReadOnlyFoo<outT>{TValue{get;}}publicinterfaceIWriteOnlyFoo<inT>{TValue{set;}}// If you program against this interface, currently you get an "ambiguity" compiler error.publicinterfaceIReadWriteFoo<T>:IReadOnlyFoo<T>,IWriteOnlyFoo<T>{}// This, however works:publicclassReadWriteFoo<T>:IReadOnlyFoo<T>,IWriteOnlyFoo<T>{TValue{get;set;}// Implements IReadOnlyFoo<T>.Value and IWriteOnlyFoo<T>.Value}
Note: This would allow us to have a call-site variance by specifying either the Read or the Write interface.
I think that I closed to early. I'm getting a compiler error when trying to access the property.
IReadWriteFoo<string>rw=newReadWriteFoo<string>();rw.Value="hello";// ==> Ambiguity between ' IReadOnlyFoo<string>.Value and // IWriteOnlyFoo<string> Value'
It looks like now we have 2 Value properties instead of one having both a getter and a setter.
However the following works, but if you want to program against interfaces it is not useful:
varrw=newReadWriteFoo<string>();rw.Value="hello";// Works if rw is typed as the class.
Result: Classes can join getters and setters, interfaces cannot.
It's interesting, if IReadWriteFoo<T> declares a shadowing property it works fine, but if it just "inherits" the members from IReadOnlyFoo<T> and IWriteOnlyFoo<T> it fails with the message that you're seeing.
This discussion was converted from issue #1031 on September 08, 2020 23:46.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
@OJacot-Descombes commented on Wed Nov 04 2015
Being able to declare separate readonly and writeonly interfaces that could be "joined" into a common read-write interface including properties and indexers would allow great interface designs.
Note: This would allow us to have a call-site variance by specifying either the Read or the Write interface.
@HaloFour commented on Wed Nov 04 2015
This is legal in C# 6.0/VS2015:
@svick commented on Wed Nov 04 2015
This already seems to work for me:
In
IReadWriteFoo<T>
, you don't need to redeclare the property, just inheriting from the base interfaces is enough:@OJacot-Descombes commented on Wed Nov 04 2015
I think that I closed to early. I'm getting a compiler error when trying to access the property.
It looks like now we have 2
Value
properties instead of one having both a getter and a setter.However the following works, but if you want to program against interfaces it is not useful:
Result: Classes can join getters and setters, interfaces cannot.
@HaloFour commented on Wed Nov 04 2015
It's interesting, if
IReadWriteFoo<T>
declares a shadowing property it works fine, but if it just "inherits" the members fromIReadOnlyFoo<T>
andIWriteOnlyFoo<T>
it fails with the message that you're seeing.@iskiselev commented on Tue Jan 17 2017
Looks like #14164 is duplicate of it, but this one is still in planning state, other one was closed with "Resolution-By Design" label.
@OJacot-Descombes commented on Wed Jan 18 2017
@iskiselev: The point of my issue is have a kind of call-site variance by specifying either the Read or the Write interface.
@jcouv commented on Sat Oct 21 2017
Moving language design discussions to csharplang. Thanks
Beta Was this translation helpful? Give feedback.
All reactions