// A callback is a function that will be called when a process is done executing // a specific task public static void Run() { // To create a callback, a function address needs to be stored in a variable WorkCompletedCallBack callBack = TestCallBack; DoWork(callBack); DoMoreWork(Console.WriteLine); // Delegates (see below) // ------------------------------------------------------ Foo fooExample = Bar; var time = fooExample(DateTime.UtcNow); Console.WriteLine(time); // Action delegate (see below) // ------------------------------------------------------ // Named method NamedActionDelegate = NamedMethod; NamedActionDelegate?.Invoke("Named Action Delegate", 5); // Anonymous function AnonymousActionDelegate?.Invoke("Anonymous Action Delegate", 106); // Func delegate (see below) // ------------------------------------------------------ NamedFuncDelegate = NamedMethod; var namedResult = NamedFuncDelegate?.Invoke(100); Console.WriteLine(namedResult); }
public static void Run() { NamedFuncDelegate = (m, n) => "Go"; var result = GetFuncStringValue(1, 2); Console.WriteLine(result); NamedFuncDelegate = NamedMethod; var addition = NamedFuncDelegate?.Invoke(5, 9); Console.WriteLine(addition); Console.WriteLine(AnonymousFuncDelegate?.Invoke(10, 12)); }
public static string GetFuncStringValue(int a, int b) { return(NamedFuncDelegate?.Invoke(a, b)); }