Skip to content

Instantly share code, notes, and snippets.

@mirmostafa
Last active December 21, 2021 09:10
Show Gist options
  • Save mirmostafa/92752c516087807b57c89d48c24a8a9b to your computer and use it in GitHub Desktop.
Save mirmostafa/92752c516087807b57c89d48c24a8a9b to your computer and use it in GitHub Desktop.
Static interface in C# 10.0
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);
}
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;
}
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