Skip to content

Instantly share code, notes, and snippets.

@sinelaw
Last active December 30, 2015 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sinelaw/7828815 to your computer and use it in GitHub Desktop.
Save sinelaw/7828815 to your computer and use it in GitHub Desktop.
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...
}
}
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