Quiz: Delegates and private methods
Given the code below, what is the result of the “Direct call” line and the “call via a delegate” lines? And, of course, why? For a little sport, try it without compiling the code first.
public delegate void Callback();
class Program
{
static void Main(string[] args)
{
MyClass my = new MyClass();
//Direct call
my.PrivateMethod();
//Call via a delegate
Callback c = my.GetCallback();
c();
}
}
public class MyClass
{
public MyClass() { }
public Callback GetCallback()
{
return new Callback(PrivateMethod);
}
private void PrivateMethod()
{
Console.WriteLine("Private Method");
}
}