Skip to content

Instantly share code, notes, and snippets.

@AlbertoMonteiro
Created January 28, 2020 16:45
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 AlbertoMonteiro/3e585c9ded719ef726f586e53b8a6888 to your computer and use it in GitHub Desktop.
Save AlbertoMonteiro/3e585c9ded719ef726f586e53b8a6888 to your computer and use it in GitHub Desktop.
Default Interface Implementation C# 8
interface ICalculator
{
int Add(int a, int b) => a + b;
}
interface ISuperCalculator
{
int Add(int a, int b) => a * 2 + b * 2;
}
public class Test : ICalculator, ISuperCalculator
{
}
var t = new Test();
if(t is ICalculator calc)
{
var a = calc.Add(1, 2);
Assert.Equal(3, a);
}
if (t is ISuperCalculator calc1)
{
var a = calc1.Add(1, 2);
Assert.Equal(6, a);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment