Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save binki/f346b3fea4ff4714ea4fe150063b2f3b to your computer and use it in GitHub Desktop.
Save binki/f346b3fea4ff4714ea4fe150063b2f3b to your computer and use it in GitHub Desktop.
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;
}
}
@binki
Copy link
Author

binki commented Oct 27, 2017

Mayhaps this is an argument that classes be sealed by default?

@Blizzardo1
Copy link

Very nice example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment