C# Delegates and Events
CNS 3260 C# .NET Software Development
C# Delegates and Events
Delegate types
A delegate declaration defines a new type
Delegates are similar to function pointers
Delegates are type-safe Delegates are object-oriented
Delegate types are derived from System.MulticastDelegate
C# Delegates and Events
The Command Pattern
Command issuer
Exposed Command Variable
Command executor
Command object instance
Knows when the event happens but doesnt know what to do about it
Knows what to do when an event happens but doesnt know when
Command Subclass Execute()
Command Class Execute()
C# Delegates and Events
Simple Delegate Command Pattern
Delegate Host Class (Publisher) Delegate User Class (Subscriber)
Subscribing Method
Exposed Delegate
Knows when the event happens but doesnt know what to do about it
Knows what to do when an event happens but doesnt know when
AKA: The Observer Pattern or .NET Event Model
C# Delegates and Events
Two reasons to use Delegates
When youre not sure what should happen when an event occurs
GUI events Threading situations Callbacks Command Pattern
To keep your interface clean
Looser coupling
C# Delegates and Events
Defining and using Delegates
three steps:
1. 2. 3.
Declaration Instantiation Invocation
C# Delegates and Events
Delegate Declaration
namespace some_namespace { delegate void MyDelegate(int x, int y);
Delegate Type Name
class MyClass { public delegate int AnotherDelegate(object o, int y); } }
C# Delegates and Events
Delegate Instantiation
delegate void MyDelegate(int x, int y); class MyClass { private MyDelegate myDelegate = new MyDelegate( SomeFun ); public static void SomeFun(int dx, int dy) { } }
Invocation Method Invocation Method name (no params or perens)
C# Delegates and Events
More Realistically
class MyClass { private MyDelegate myDelegate; public MyDelegate MyDelegate { set { this.myDelegate = value; } } } class MainClass { [STAThread] static void Main() { MyClass mc = new MyClass(); mc.MyDelegate = new MyDelegate( MainClassMethod ); } private static void MainClassMethod(int x, int y) { } }
Delegate Variable (null)
Passed in and assigned in the constructor
C# Delegates and Events
Instance Methods
class MyClass { private MyDelegate myDelegate; public MyDelegate MyDelegate { set { this.MyDelegate = value; } } } class MainClass an instance { [STAThread] static void Main() { MainClass mainClass = new MainClass(); MyClass mc = new MyClass(); mc.MyDelegate = new MyDelegate( mainClass.MainClassMethod ); } private void MainClassMethod(int x, int y) { } }
Method attached to
C# Delegates and Events
Delegate-Method Compatibility
A Method is compatible with a Delegate if
They have the same parameters They have the same return type
C# Delegates and Events
Delegate Invocation
class MyClass { private MyDelegate myDelegate; public MyClass(MyDelegate myDelegate) { this.MyDelegate = myDelegate; } private void WorkerMethod() { int x = 500, y = 1450; if(myDelegate != null) myDelegate(x, y); }
Attempting to invoke a delegate instance whose value is null results in an exception of type System.NullReferenceException.
C# Delegates and Events
Delegates Multicast Nature
Delegate is really an array of function pointers
mc.MyDelegate += new MyDelegate( mc.Method1 ); mc.MyDelegate += new MyDelegate( mc.Method2 ); mc.MyDelegate = mc.MyDelegate + new MyDelegate( mc.Method3 );
Now when Invoked, mc.MyDelegate will execute all three Methods
Notice that you dont have to instantiate the delegate before using +=
The compiler does it for you when calling +=
C# Delegates and Events
The Invocation List
Methods are executed in the order they are added Add methods with + and += Remove methods with - and -=
Attempting to remove a method that does not exist is not an error
Return value is whatever the last method returns A delegate may be present in the invocation list more than once
The delegate is executed as many times as it appears (in the appropriate order) Removing a delegate that is present more than once removes only the last occurrence
C# Delegates and Events
Multicast example
mc.MyDelegate = new MyDelegate( mc.Method1 ); mc.MyDelegate += new MyDelegate( mc.Method2 ); mc.MyDelegate = mc.MyDelegate + new MyDelegate( mc.Method3 );
// The call to: mc.MyDelegate(0, 0); // executes:
// mc.Method1 // mc.Method2 // mc.Method3
(See Delegates Demo)
C# Delegates and Events
System.Delegate
abstract Special
Only the system and compiler can inherit from System.Delegate Method
Members:
Returns A MethodInfo object The this pointer belonging to the method null if the method was static
Target
operator == and operator !=
C# Delegates and Events
System.MulticastDelegate
Target Method operator== and operator != _prev
Linked list of System.Delegate objects Returns the Invocation List as a Delegate[]
GetInvocationList()
C# Delegates and Events
Delegate Dangers
Exposing a delegate might be bad if:
You dont want others to invoke your delegate You have assigned to the delegate internally
Enter the event keyword...
C# Delegates and Events
Events
Events are safe delegates
But they are delegates
Restricts use of the delegate (event) to the target of a += or -= operation
No assignment No invocation No access of delegate members (like GetInvocation List) Event Accessors
Allow for their own Exposure
C# Delegates and Events
Event Accessors
public delegate void FireThisEvent(); class MyEventWrapper { private event FireThisEvent fireThisEvent; public void OnSomethingHappens() { if(fireThisEvent != null) fireThisEvent(); } public event FireThisEvent FireThisEvent { add { fireThisEvent += value; } remove { fireThisEvent -= value; } } }
add and remove keywords
(See Event Demo)
C# Delegates and Events
On Word on Exceptions
Exceptions thrown from a delegate:
Are thrown to the calling context All subsequent delegates in the Invocation List are ignored
C# Delegates and Events
Library Delegates
ThreadStart TimerCallback ASyncCallback EventHandler KeyPressEventHandler KeyEventHandler etc.
C# Delegates and Events