What is the benefit of calling the method indirectly via a pointer?
Dictionary def - Delegate: is a representative to communicate between two parties.
class Program { static void Main(string[] args) { MyClass obj = new MyClass(); obj.LongRunning(Callback); } static void Callback(int i) { Console.WriteLine(i); } } public class MyClass { public delegate void CallBack(int i); public void LongRunning(CallBack obj) { for (int i = 0; i < 10000; i++) { obj(i); } } }
Program Flow
1. Call LongRunning passing Callback method.2. LongRunning method within MyClass is executed and executes passed in method which is now called obj but in reality it is a Pointer to the Callback method.
3. Callback method is executed on Program class, which writes the current value of i to the console.
my thanks to:
https://www.youtube.com/watch?v=ifbYA8hyvjc
http://www.codeproject.com/Articles/1061085/Delegates-Multicast-delegates-and-Events-in-Csharp