Last active
December 30, 2015 12:28
-
-
Save sinelaw/7828815 to your computer and use it in GitHub Desktop.
More code related to this StackOverflow question: http://stackoverflow.com/questions/20412783/why-is-a-property-get-considered-ambiguous-when-the-other-interface-is-set-only/20426619
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface IGetMethod { int GetValue(); } | |
interface ISetMethod { void SetValue(int value); } | |
interface IBothMethods : IGetMethod, ISetMethod { } | |
class BothMethods : IBothMethods | |
{ | |
int _value; | |
public int GetValue() { return _value; } | |
public void SetValue(int value) { _value = value; } | |
} | |
class Test { | |
void DoTest(IBothMethods a) { | |
var x = a.GetValue(); // no ambiguity here... | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface IGetMethod { int GetValue(); } | |
interface ISetMethod { void SetValue(int value); } | |
interface IBothMethods : IGetMethod, ISetMethod { } | |
class BothMethodsWithExplicit : IBothMethods | |
{ | |
int _value; | |
int _valueExplicit; | |
public int GetValue() { return _value; } | |
public void SetValue(int value) { _value = value; } | |
int IGetMethod.GetValue() { return _valueExplicit; } | |
void ISetMethod.SetValue(int value) { _valueExplicit = value; } | |
} | |
class Test { | |
void DoTest() { | |
BothMethodsExplicit a = new BothMethodsExplicit(); | |
a.SetValue(2); | |
IGetMethod getA = a; | |
Assert.AreEqual(2, a.GetValue()); | |
Assert.AreEqual(0, getA.GetValue()); | |
ISetMethod setA = a; | |
setA.SetValue(3); | |
Assert.AreEqual(3, getA.GetValue()); | |
Assert.AreNotEqual(a.GetValue(), getA.GetValue()); | |
IBothMethods ia = a; | |
Assert.AreNotEqual(a.GetValue(), ia.GetValue()); | |
Assert.AreEqual(getA.GetValue(), ia.GetValue()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment