C# delegates have always been difficult for me to grasp and so I was very happy to stumble across logicchild's article on The Code Project web site titled "C# Delegates: Step by Step". He has a very succinct way of explaining C# delegates and I can recommend it to you. However, in trying out the examples, I see that are two ways to initialize a delegate, mainly:
//create a new instance of the delegate class
CalculationHandler sumHandler1 = new CalculationHandler(math.Sum);
//invoke the delegate
int result = sumHandler1(8, 9);
Console.WriteLine("Result 1 is: " + result);
and
CalculationHandler sumHandler2 = math.Sum;
//invoke the delegate
int result = sumHandler2(8, 9);
Console.WriteLine("Result 2 is: " + result);
where the math class is defined as
public class Math
{
public int Sum(int x, int y)
{
return x + y;
}
}
So which is the "correct" way and why?