as for as my knowledge is conserned delegate used to know the methods at runtime but the same purpose can be provided by virtual method ,as i am new to c#.net provide the analogy regarding this querry
Loading
as for as my knowledge is conserned delegate used to know the methods at runtime but the same purpose can be provided by virtual method ,as i am new to c#.net provide the analogy regarding this querry
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Tom DiannPosted Dec 28, 2005, 3:10 AM
AnttiPosted Dec 22, 2005, 11:46 AM
Um, delegate is kinda like function pointer. You create a delegate and add a function name in it, and you can call the function using the delegate. You can delegate tasks to different functions, as the name says. You can change the delegates method in runtime, so same call can perform various functionalities.
Delegates are also usefull when making asynchronous calls in windows applications, and you have update your UI from another process, from another thread for example. You can't directly call UI methods from another thread, but you must invoke the UI update calls using delegates. I'm not sure if this is the case in Desctop Windows, but in Windows CE you have to do it this way.
//here you declare a delegate
delegate void YourDelegateType(string text);
//there you create a delegate oject, that is used to call methods,
//and add a method in it
YourDelegateType myDelegate = new YourDelegateType( SomePrintingMethod );
//here's the printing method, notice that delegate can only use
//methods with same parameters and return type
void SomePrintingMethod(string text)
{
    Console.WriteLine(text);
}
//for example in windows application you can use delegate like this:
this.Invoke( myDelegate, "HelloWorld" );
But virtual functions have little to do with delegates. Virtual function is a object oriented programming concept, that enables the derived classes to alter the behaviour of the superclass methods. For example: You have a class called Cat. Cat class has a virtual method called DoMeaow(), which prints text "Meaow!" to the screen. Then You derive from Cat class a new class called Citten. There you can override superclass' DoMeaow() method, and do different kind of meaowing there, for example "Miumiu. I'm a little cat.".
method ~= function