static void Main(string[] args) { TakeIntReturnIntDelegate method1 = delegate(int v) { return(v + 100); }; DoSomethingWithDelegate(method1); DoSomethingWithDelegate(delegate(int x) { return(x / 2); }); }
static void Main(string[] args) { // Doesn't work //TakeIntReturnIntDelegate del = Foo; // Hm... shouldn't work, but does :) - // Impossible to configure the C# compiler these days to NOT understand this. TakeIntReturnIntDelegate del = Square; // How it should have been in C# 1.0: del = new TakeIntReturnIntDelegate(Square); Console.WriteLine(del(13)); // Delegates are types - do with them what you can do with other types. DoSomethingWithDelegate(del); }
static void DoSomethingWithDelegate(TakeIntReturnIntDelegate del) { Console.WriteLine(del(5) + del(8)); }
static void DoSomething(TakeIntReturnIntDelegate function) { Console.WriteLine(function(10)); }