I want to know the difference between using Delegate Methods and using General Methods[without Delegates].
For Example :
With Delegate :
delegate void DelMethod(string str);
static void Method(string str)
{
Debug.WriteLine(str);
}
Usage :
DelMethod dm = new DelMethod(Method);
dm(string);
And Without Delegate :
static void Method(string str)
{
Debug.WriteLine(str);
}
Usage :
Method(string)
What are the differences of these two??
The method without delegate is smaller and easy. But I find coders using delegated Methods frequently.
What is the reason behind this??
Delegates are for another situation. Imagine, that you have a class which should answer for something from another class, but you know nothing about the second class. In such situation you can do a Delegate in the first.
Class Aknows nothing aboutclass B, but it can call B’s methods and get it’s results. TheAnswermethod inclass Bis private andclass Acan’t call it directly.Read more in MSDN