Created
January 28, 2020 16:45
-
-
Save AlbertoMonteiro/3e585c9ded719ef726f586e53b8a6888 to your computer and use it in GitHub Desktop.
Default Interface Implementation C# 8
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
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