0% found this document useful (0 votes)
48 views13 pages

Events & Delegates in C#

Delegates allow methods to be passed as arguments in C#. A delegate defines a signature that specifies the return type and parameters of methods that can be associated with the delegate. When a delegate is invoked, it calls any methods that have been added to its invocation list. Events use delegates to allow one object to notify other objects when something happens by maintaining a list of event handler methods. Delegates and events provide a way to decouple classes and allow for more flexible, reusable code.

Uploaded by

eshamu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views13 pages

Events & Delegates in C#

Delegates allow methods to be passed as arguments in C#. A delegate defines a signature that specifies the return type and parameters of methods that can be associated with the delegate. When a delegate is invoked, it calls any methods that have been added to its invocation list. Events use delegates to allow one object to notify other objects when something happens by maintaining a list of event handler methods. Delegates and events provide a way to decouple classes and allow for more flexible, reusable code.

Uploaded by

eshamu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Events & Delegates in C#

Delegates
Define a signature (return type and arguments) of acceptable callback methods Instantiated by wrapping the callback method Maintain a list of methods to be called when delegate is invoked Just like function pointers

delegate int MathOp(int x, int y);

// Declare delegate

class MyClass { static void Main() { // Instantiate delegates MathOp addOp = new MathOp(MyClass.Add); MathOp mulOp = new MathOp(MyClass.Multiply); Console.WriteLine(PerformOp(addOp, 3, 4)); Console.WriteLine(PerformOp(mulOp, 3, 4)); } public static int PerformOp(MathOp op, int x, int y) { return op(x,y); // Invoke delegate } public static int Add(int x, int y) { return x + y; }

class MyClass { private string name; private int number; public MyClass( string name, int number) { this.name = name; this.number = number; }
public void ShowMyName() { Console.WriteLine(My name is + this.name); } public void ShowMyNumber() { Console.WriteLine(My number is + this.number); }

delegate void DemoOp();

// declare delegate type

static void Main() { // create some instances of MyClass MyClass mc1 = new MyClass(First, 1); MyClass mc2 = new MyClass(Second, 2); DemoOp demo = null; // declare an empty delegate

// append methods to list demo += new DemoOp(mc1.ShowMyName); demo += new DemoOp(mc2.ShowMyNumber); demo(); // invoke delegate } My name is First My number is 2

Delegates Behind the Scenes


demo

target method ptr next


target method ptr next

mc1 ShowMyName

mc2 ShowMyNumber

Problem: General Sort


Want to sort an array of integers Can sort both ascending and descending order Write the sort method just once How can delegates help?

Problem: General Sort


Want to sort an array of integers Can sort both ascending and descending order Write the sort method just once How can delegates help?

delegate bool SwapCondition(int a, int b);

bool LessThan(int a, int b) ... bool GreaterThan(int a, int b) ...


void Sort(int[] ary, SwapCondition sc) { ... if (sc(ary[i], ary[j])) ...

.NET Event Model


Events let one object tell another that something has happened
Event-Generating Object Event Handler Event Event-Consuming Objects

Objects register their event handlers for the event

Event Handler

.NET Event Model


Events let one object tell another that something has happened
Event-Generating Object Event Handler Event Event-Consuming Objects

When the event fires, the handlers get called

Event Handler

Event Handlers
// defined for us delegate void EventHandler(Object sender, EventArgs e);

Object sender Event-generating object

EventArgs e Additional information about the event

Event Handlers
// defined for us delegate void EventHandler(Object sender, EventArgs e); using System.Windows.Forms; class MyForm : Form { private Button myButton; public MyForm() { myButton = new Button(); myButton.Text = Click Me; Controls.Add(myButton); myButton.Click += new EventHandler(MyClick); } public void MyClick(Object sender, EventArgs e) { myButton.Text = Thank You; } }

Custom Events
Define the event argument class Define delegate type for custom event handler Define the event Write an event handler (callback function) Subscribe to the event Fire the event when appropriate

You might also like