Delegates are not just related to threading, you can use them at any time - and do, although you don't always realise it.
A delegate is a method reference (what would be called a function pointer in other languages) - it allows you to set up a method to be called at a later date. When you call the method, you do not need to know what it is called - you just execute it from the delegate. It is the whole mechanism on which events work!
Why do we use them? Because they can be faster (you don't need to do "if this then that, else theOther", you can so go DoIt and run the preset method). Because they can make methods more generic: a delegate with can be loaded with as Console or TextBox write method can be called throughout a method, or class, and the same code handles both functions.
"it is said they at design time you dont know the function name, so you use delegate.?
even then when you create delegate to point to all the methods that satisfies some catagory you do it by
public delegate void Name (string a);
rite here it is pointing all the mmethods that have same signate like this one.
but when you execute it?
Name object = new object(method name);
here you are spacifing the name of the method?
rite?
so as i said we dont know the name of the method than how could we?
we could not execute it withot its name referenced.
please explian."
A delegate does not point at all teh methods, it doesn't point at
anything until you give it a value. In the same way that to use a class variable, you must tell it which instance of the class it refers to, you have to tell the delegate which method it refers to:
private delegate int MyDelegate(string s);
private MyDelegate execute = null;
private int total = 0;
private void ShowDelegate()
{
execute = UseNameSmith;
DoDelegate();
execute = UseNameJones;
DoDelegate();
Console.WriteLine(total);
}
private void DoDelegate()
{
if (execute != null)
{
total += execute("Hello");
}
}
private int UseNameSmith(string s)
{
Console.WriteLine(s + " Smith");
return 1;
}
private int UseNameJones(string s)
{
Console.WriteLine(s + " Jones");
return 2;
}
The first line declares the delegate - it defines what method signature it can accept. If you try to assign a method that has different parameters, it will complain!
The second declares the delegate variable - this is where you store the method instance you want to call.
The rest just shows what happens if you use a delegate.
The important thing to notice is that the DoDelegate method does not change - it is tested, working code, so we do not have to alter it in any way to get similar but different results. This is one of the big advantages of delegates: flexibility combined with reliability. Provided the methods you assign to the delegate variable do what they are supposed to, the original, working code does not need to be changed to provide increased functionality.
Because delegates are method references, you can do some clever things with them, that improve performance. For example, if you wanted to execute a different method based on what enum value was passed to a method, you could go:
if (value == myEnum.firstOption)
executeMethodOne();
else if (value == myEnum.secondOption)
exeecuteMethodTwo();
else ...
But that is a lot of code, and a lot of tests!
You could do it with a switch:
switch (value)
{
default:
break;
case myEnum.firstOption:
executeMethodOne();
break;
case myEnum.secondOption:
exeecuteMethodTwo();
break;
...
}
Or, if you set up your enum correctly (and do some sensible bounds checking) you could set up an array of delegates and do it in one line:
private MyDelegate[] myTable = new MyDelegate[] ;
....
myTable[value]("Goodbye");
Ok, you have to set up the methods in the class constructor, or use static methods only to set them up at compile time, but you save time when you execute it, and that is generally more important.
(That, by the way, is an oversimplification - the compiler does pretty much that with a well designed switch anyway)