Last active
December 21, 2021 09:10
-
-
Save mirmostafa/92752c516087807b57c89d48c24a8a9b to your computer and use it in GitHub Desktop.
Static interface in C# 10.0
This file contains 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
public interface IMath | |
{ | |
static int Add(int x, int y) => x + y; | |
abstract static int Sub(int x, int y); | |
abstract static int Mul(int x, int y); | |
abstract static int Div(int x, int y); | |
} |
This file contains 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
internal class Math : IMath | |
{ | |
public static int Div(int x, int y) => x - y; | |
public static int Mul(int x, int y) => x * y; | |
public static int Sub(int x, int y) => x / y; | |
} |
This file contains 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
internal partial class Program | |
{ | |
public static void Main() | |
{ | |
var sum = Add<Math>(5, 6); | |
System.Console.WriteLine(sum); | |
var sub = Sub<Math>(6, 5); | |
System.Console.WriteLine(sub); | |
} | |
public static int Add<TMath>(int x, int y) | |
where TMath : IMath => TMath.Add(x, y); | |
public static int Sub<TMath>(int x, int y) | |
where TMath : IMath => TMath.Sub(x, y); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment