Click here to Skip to main content
15,892,797 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
So i just started studying C# and realised the actual necessity of C#.
Please provide me an example in C# such that the example demonstrates something which can be done by a delegate but cannot be done by a function call something like the following

public void ReplicateDeligate(int x,int y)
   {
   Fun1(x,y);//defined elsewhere (May be a normal method,class obj etc)
   Fun2(x,y);//Defined elsewhere
   MyClass.StatClassFun(x,y);

   //.
   //.
   //Etc and so on
   }


Which i guess can be done by the delegate. So what is the difference.
Does using delegates give better execution speed with less memory consumption? ??

[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 27-Jun-11 21:09pm
v2

You should just have googled[^] it.

Seeing your sample I would suggest you to learn from http://msdn.microsoft.com/en-us/library/ms173171(v=vs.80).aspx[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-Jun-11 3:12am    
Makes sense, my 5.
I can offer my original works; please see my answer; many say they are interesting.
--SA
For example, you may want to see my article: Dynamic Method Dispatcher[^]. In particular, see section 6 which shows something not resolved elegantly enough with other methods.

Another short article Tips/Trick article is Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^], see the code sample where queue type is a delegate type.

All this has nothing to do with performance of memory consumption (which are quite good). Delegate is additional abstraction tool adding a lot of flexibility compared to older versions of OOP before introduction of delegates.

Please see also the following interesting discussions (with my answers, with some examples):
Pass a method with N params as parameter in C#[^],
What are the advantages of delegates in C#.NET?[^],
Confusion about delegates[^].

—SA
 
Share this answer
 
v3
Comments
Abhinav S 28-Jun-11 3:12am    
Good answer. My 5.
Sergey Alexandrovich Kryukov 28-Jun-11 3:14am    
Thank you, Abhinav. (I'm adding some more links.)
--SA
CPallini 28-Jun-11 3:17am    
Fived.
Sergey Alexandrovich Kryukov 28-Jun-11 11:31am    
Thank you.
--SA
jayantbramhankar 28-Jun-11 3:48am    
Good answer , my five
Delegates 101 - Part I: What is a Delegate?[^]

This is the best solution ever i seen.
 
Share this answer
 
Delegates are different from normal methods in that they allow you to call them without knowing which method you are calling at compile time. They don't do anything that can't be done with normal method calls, but they do allow code to be simpler and cleaner:

enum function
   {
   callPrintScreen,
   callPrintPrinter,
   callPrintPDF
   }
private function func;
...
   func = function.callPrintPDF;
...
   switch (func)
      {
      case function.callPrintScreen:
         PrintScreen(myString);
         break;
      case function.callPrintPrinter:
         PrintPrinter(myString);
         break;
      case function.callPrintPDF:
         PrintPDF(myString);
         break;
   }
Becomes:
        private delegate void DoFunc(string s);
        private DoFunc func; 
...
            func = PrintPDF;
...
            func(myString);
If you need to add a new option (say PrintConsole), then you don't have to change the code which uses it at all - just the code that loads the delegate.
 
Share this answer
 
You can pick up a good book on C# and go through the chapter on delegates. That should help you get started.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900