static void Main(string[] args) { //PrintToConsole("hello there!"); DelegateToPrint functionToPrint = PrintToConsole; functionToPrint("hello there!"); }
static void Main(string[] args) { Console.WriteLine(DateTime.Now.Second); System.Threading.Thread.Sleep(2000); Console.WriteLine(DateTime.Now.Second); Console.WriteLine(); Program.PrintToConsole("Hello World!"); DelegateToPrint functionToPrint = PrintToConsole; functionToPrint("Hello World From a delegate function!"); Action <string> actionPrint = PrintToConsole1; DelegateToPrint functonDoNothing = DoNothing; functonDoNothing("Nothing"); Program.PrintStringByFunction(functionToPrint, "Hello from delegate passed as a parameter"); Console.WriteLine(); DelegateToPrint delegateToPrint1 = PrintToConsole1; delegateToPrint1("Hello"); DelegateToPrint delegateToPrint2 = PrintToConsole2; delegateToPrint2("Hello"); Console.WriteLine(); DelegateToPrint delegateToPrintChain = PrintToConsole1; delegateToPrintChain += PrintToConsole2; delegateToPrintChain += PrintToConsole3; delegateToPrintChain("Hello"); delegateToPrintChain += PrintToConsole2; delegateToPrintChain -= PrintToConsole2; Console.WriteLine(); var functionsUsedByDelegate = delegateToPrintChain.GetInvocationList(); foreach (var del in functionsUsedByDelegate) { Console.WriteLine(del); } Console.WriteLine(); delegateToPrint1.Invoke("Invoke method used"); delegateToPrint1?.Invoke("Not null"); }
private static void PrintStringByFunction(DelegateToPrint delegateToPrint, string stringToPrint) { delegateToPrint(stringToPrint); }