static void Main(string[] args) { //creating an object to able pointing the delegate to the Add method PointToAddFunction pointToAdd = Add; Console.WriteLine("Using Add method:"); Console.WriteLine(pointToAdd(2, 3)); //Creating an object with an anonymous method //Does the same thing as Add function above without declaring a name PointToAddFunction pointToAnonymous = delegate(int num1, int num2) { return(num1 + num2); }; Console.WriteLine("Using an anonymous method"); Console.WriteLine(pointToAnonymous(4, 5)); }
public void MainFunc() { Console.WriteLine("Test Performance with delegate to function"); Stopwatch stopwatch = new Stopwatch(); for (int x = 0; x < 10; x++) { stopwatch.Reset(); stopwatch.Start(); for (int y = 0; y < 1000; y++) { PointToAddFunction pointAdd = Add; pointAdd.Invoke(2, 2); } stopwatch.Stop(); Console.WriteLine(stopwatch.ElapsedTicks.ToString()); } Console.WriteLine(); Console.WriteLine(); Console.WriteLine("***************"); Console.WriteLine("Test Performance with delegate to Anonimus function"); for (int x = 0; x < 10; x++) { stopwatch.Reset(); stopwatch.Start(); for (int y = 0; y < 1000; y++) { PointToAddFunction pointAdd = delegate(int num1, int num2) { return(num1 + num2); }; pointAdd.Invoke(2, 2); } stopwatch.Stop(); Console.WriteLine(stopwatch.ElapsedTicks.ToString()); } }