Skip to content

Instantly share code, notes, and snippets.

@kumpera
Created August 17, 2018 13:57
Show Gist options
  • Save kumpera/5541e79c4564aaaffcb10132e4e6ca65 to your computer and use it in GitHub Desktop.
Save kumpera/5541e79c4564aaaffcb10132e4e6ca65 to your computer and use it in GitHub Desktop.
Traits for C#
interface BinaryOp {
int Apply (int a, int b);
}
struct Multiply {
public int Apply (int a, int b) {
return a * b;
}
}
static void Main () {
var m = new Multiply ();
var t = Trait<BinaryOp>.Make (ref m); //takes a ref, no boxing.
var res = t.As ().Apply (10, 20); // == 200
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment