Invoking A Delegate

Akiii
Posted by Akiii under C# category on | Points: 40 | Views : 993
Hi,
Today i will tell you two ways to invoke a Delegate. First, i should give you a sample method :-


public static void Method1(string msg)
{
Console.WriteLine("Method1 : " + msg);
}


Now, we declare a Selagate :-

public delegate void TestDelegate(string msg);


Now, calling the method with the help of a Delegate object :-


static void Main(string[] args)
{
TestDelegate td = new TestDelegate(Method1);
td("hello");
td.Invoke("hello");

}


In the above code, either you can use "Invoke" method or by directly calling the method with help of passing the parameter.


Please note that declaring a Delegate should be the first thing you do ! Here i am just siting an example, so please adhere to standards !



Thanks and Regards
Akiii

Comments or Responses

Login to post response