Created
October 27, 2017 18:47
-
-
Save binki/f346b3fea4ff4714ea4fe150063b2f3b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
using System; | |
class Program | |
{ | |
static void Main() | |
{ | |
var b = new Base(); | |
b.DoSomething(); | |
// The answer is 42! | |
var s = new Sub(); | |
s.DoSomething(); | |
// Sub observed secret value: 21 | |
// The answer is 0! | |
} | |
} | |
interface IBlah | |
{ | |
int GetSomeNumber( | |
int someSecret); | |
} | |
class Base : IBlah | |
{ | |
public void DoSomething() | |
{ | |
// Get explicit implementation. | |
IBlah blah = this; | |
var number = blah.GetSomeNumber(21); | |
Console.WriteLine($"The answer is {number}!"); | |
} | |
int IBlah.GetSomeNumber(int someSecret) => 2 * someSecret; | |
} | |
class Sub : Base, IBlah | |
{ | |
// Change a behavior of the base class by replacing its | |
// IBlah implementation. | |
int IBlah.GetSomeNumber(int someSecret) | |
{ | |
Console.WriteLine($"{GetType().Name} observed secret value: {someSecret}"); | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very nice example