fb

Ads

Pages

create a delegate

A delegate declares a ref type that references a named of anonymous method. Delegates
are secure and type-safe. Consider them as type safe function pointers.
 


public delegate void Del<T>(T item);
Del<int> d1 = new Del<int>(Notify);

Declare and raise events from your application.
Declare Events: “Event” keyword is used to declare an event. public delegate void MyCustomHandler(object o, MyEventArgse);
public class MyEventArgs: EventArgs
{
public readonly int Age;
public MyEventArgs(int age)
{
Age = age;
}
}
public class MyCustomListener
{
public void Show(object o, MyEventArgs e)
{
Console.WriteLine(
"Age is {0}",
e.Age);
}
}


Implement event handlers and associate them with events.


public class MyClass
{
public static event MyCustomHandler MyEvent;
public static void Main()
{
MyCustomListener mcll = new MyCustomListener();
MyEvent += new MyCustomHandler(mcl1.Show);
GetAge();
}
public static void OnMyEvent(MyEventArgse)
{
if(MyEvent!=null)
MyEvent(new object(),e);
}
public static void GetAge()
{
MyEventArgse1 = new MyEventArgs(25);
OnMyEvent(e1);

}
}

0 comments:

Post a Comment