Posted: 10/25/2010
Hello Experts,
I have created a simple Delegates.Example -
using System; using System.IO; namespace ConsoleApplication2 { public class MyClass { public delegate void MyDelegate(); public void Process(MyDelegate myDelegate) { if (myDelegate!=null) { Console.WriteLine("Delegate"); } } } public class TestApplication { public void MyTest() { Console.WriteLine("My Test Method"); } static void Main(string[] args) { TestApplication testApplication = new TestApplication(); MyClass myClass = new MyClass(); MyClass.MyDelegate myDelegate = new MyClass.MyDelegate(testApplication.MyTest); myClass.Process(myDelegate); Console.ReadLine(); } } }
Output -
Delegate
Can anyone tell me why My Test Method is not print?
Posted: 2/8/2011
I really don't know how we missed this out.
Emm... it's because you actually call the Process(delegate) method explicitly. Since delegate won't be null as you already add myDelegate as parameter to the Process method, the "Delegate" will be print. You don't really call MyTest() method.
It will be printed, if you add
public MyClass() { TestApplication t = new TestApplication(); t.MyTest(); }
Inside MyClass.
Hope this helps.
Posted: 2/10/2011
Thank you Hajan.
mohit kumar said: Thank you Hajan.
You are always welcome !