c# - Why to use delegates in .Net -


i reading article describing use of delegates following example shows use of multicast delegate

 public delegate void progressreporter(int percentcomplete); class program {     static void main(string[] args)     {         progressreporter p = writeprogresstoconsole;         p += writeprogresstofile;         utility.hardwork();     }      private static void writeprogresstoconsole(int percentcomplete)     {         console.writeline(percentcomplete);     }      private static void writeprogresstofile(int percentcomplete)     {                 system.io.file.writealltext("progress.txt", percentcomplete.tostring());                } }     public static class utility {     public static void hardwork(progressreporter p)     {         (int = 0; < 10; i++)         {                  p(i);             system.threading.thread.sleep(1000);         }     } } 

but understanding of code think same can done using class , having same functions define tasks done delegate handlers follows

 public static class progressreporter {     public static void writeprogresstoconsole(int percentcomplete)     {         console.writeline(percentcomplete);     }      public static void writeprogresstofile(int percentcomplete)     {         system.io.file.writealltext("progress.txt", percentcomplete.tostring());     } } 

and changing utility class hardwork() follows

 public static class utility {     public static void hardwork()     {         (int = 0; < 10; i++)         {             progressreporter.writeprogresstoconsole(i * 10);             progressreporter.writeprogresstofile(i * 10);             system.threading.thread.sleep(1000);         }     } } 

so question respect code is, why need delegate in first place?

some of reasons(plz correct if wrong) think need delegate follows-

  1. if need notification in program class itself, need delegates.
  2. with of multicast delegate can call multiple functions @ same time in place of calling them multiple times(as in second case).

a delegate way have reference particular method variable, meaning can change, instead of last example, hardcoding program methods call.

are there way without delegates? sure, can provide objects override methods or use classes implements interfaces, delegates cheaper in sense don't need whole type wrapped around single method.

examples of situations hardcoding won't do, , interfaces/overriding methods more work delegates, try looking @ visual components , events. events in .net use delegates. can double-click on button in visual designer in visual studio , create method , wire event way of delegate. having create class, or implement interface on top of form class lot more work, , if have multiple buttons want different things, need multiple objects implementing interfaces.

so delegates have place, examples doesn't them justice.

here linqpad example demonstrates 1 method (dosomething) can end doing different things depending on delegate provided it:

void main() {     dosomething(msg => console.writeline(msg));      using (var writer = new streamwriter(@"d:\temp\test.txt"))     {         dosomething(msg => writer.writeline(msg));     } }  public delegate void logdelegate(string message);  public static void dosomething(logdelegate logger) {     logger("starting");     (int index = 0; index < 10; index++)         logger("processing element #" + index);     logger("finishing"); } 

this first log console, rerun method , log file.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -