c# - Delagate example whats the point -


like many other posts i've found on so, i'm trying head around delegates. example not classed duplicate because asking specific question particular example.

 public delegate void hellofunctiondelegate(string message);      public class delegate     {         static void main()         {             hellofunctiondelegate del = new hellofunctiondelegate(goodnight); // delegate point goodnight method              del("hello"); // invoke delegate         }          public static void goodmorning(string strmessage)         {             console.writeline(strmessage + " , morning!");             console.readkey();         }          public static void goodnight(string strmessage)         {             console.writeline(strmessage + " , night!");             console.readkey();         }     } 

so in example understand delegate reference function matches signature , if pass in goodmorning see: hello , morning!

and if pass in goodnight see: hello , night!

so kind of going through middle man...

i don't understand what's point, why wouldn't directly call goodmorning / goodnight methods , when need use them?

maybe there better examples when delegate useful, in example, why don't bypass middle man?

since asking concretely example , not in general: there no point doing in particular piece of code. teaches mechanics of delegates not teach point of using them.

in short, point piece of code can take reference method without knowing method receive. can later call delegate @ will. enables more abstractions otherwise possible.


Comments

Popular posts from this blog

gridview - Yii2 DataPorivider $totalSum for a column -

java - Suppress Jboss version details from HTTP error response -

Sass watch command compiles .scss files before full sftp upload -