static void Main(string[] args) { // ===== case 1 (Simple Delegate)========== // instantiate the delegate (either this way) Console.WriteLine("===== case 1 (Simple Delegate)=========="); DSayAnything obj = new DSayAnything(SayHello); // invoke/call the delegate obj("Sumit"); // instantiate the delegate (or this way) //DSayAnything obj2 = SayGoodBye; // invoke/call using invoke() method // obj2.Invoke("Yogesh"); Action <string, string> obj8 = SayGoodBye; obj8("Sehwag", "Virendra"); // case 6 Func <string, bool> f1 = SaySomething; Console.WriteLine(f1("Mika")); // ===== case 2 (Multicast Delegate)========== /* Console.WriteLine("===== case 2 (Multicast Delegate)=========="); * DSayAnything obj3 = SayHello; * obj3 += SayGoodBye; * obj3.Invoke("Ramesh");*/ // ===== case 3 (Pass delegate as a parameter)========== Console.WriteLine("===== case 3 (Pass delegate as a parameter)=========="); TakeDelegateAsParameter(SayHello); Console.WriteLine(); // ===== case 4 (Anonymous Method)========== Console.WriteLine("===== case 4 (Anonymous Method)=========="); TakeDelegateAsAnonymouseMethod(delegate(string name) { Console.WriteLine("Good morning! " + name + "\n"); }); // ===== case 5 (Lambda Expression for anonymous method)========== Console.WriteLine("===== case 5 (Lambda Expression for anonymous method)=========="); DSayAnything obj6 = (name) => Console.WriteLine("Good Night! " + name + "\n"); obj6("Paresh"); }
public static void Case3() { DSayAnything x = (name) => { Console.WriteLine("Inside lambda fuction"); Console.WriteLine("Hello " + name); }; x("Ramesh"); }
public static void Case2() { DSayAnything x = delegate(string name) { Console.WriteLine("Inside anonymous fuction"); Console.WriteLine("Hello " + name); }; x("Suresh"); }
public static void MessageWizard(DSayAnything fn) { Console.WriteLine("Inside Message Wizard"); fn("champ"); }
public static void MessageForCustomTesting(DSayAnything func) { Console.WriteLine($"Custom Message"); func("Vishal is trying to something Different,May god guide him"); }
public static void Case1() { DSayAnything x = InsideNameFunc; x("Sumit"); }
public static void TakeDelegateAsAnonymouseMethod(DSayAnything obj) { Console.Write("Call the anonymous method in another method : "); obj("Dipak"); }
public static void TakeDelegateAsParameter(DSayAnything obj4) { Console.Write("Call the method in another method : "); obj4("Raju"); }