// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; namespace E { public class NonApplicable { public int Add(int x, int y) { return x + y; } public int Sub(int x, int y) { return x - y; } public void Test() { int x = 10; int y = 5; int r1 = Add(x, y); int r2 = Sub(x, y); Console.WriteLine(r1 + " " + r2); } } public class TypeSafe { public delegate int Adder(int v1, int v2); public delegate int Subber(int v1, int v2); public int Test2(int x, int y, int z, Adder add, Subber sub) { return sub(add(x, y), z); } public void Test() { Adder add = (_x, _y) => _x + _y; Subber sub = (_x, _y) => _x - _y; int x = 10; int y = 5; int r1 = add(x, y); int r2 = sub(x, y); Console.WriteLine(r1 + " " + r2); Console.WriteLine(Test2(0, x, y, add, sub)); } } public class NotTypeSafe { public int Test2(int x, int y, int z, Func add, Func sub) { return sub(add(x, y), z); } public void Test() { Func add = (_x, _y) => _x + _y; Func sub = (_x, _y) => _x - _y; int x = 10; int y = 5; int r1 = add(x, y); int r2 = sub(x, y); Console.WriteLine(r1 + " " + r2); //Console.WriteLine(Test2(0, x, y, add, sub)); Console.WriteLine(Test2(0, x, y, sub, add)); } } public class Program { public static void Main(string[] args) { NonApplicable o1 = new NonApplicable(); o1.Test(); TypeSafe o2 = new TypeSafe(); o2.Test(); NotTypeSafe o3 = new NotTypeSafe(); o3.Test(); } } }