Sie sind auf Seite 1von 11

Delegates & Events

Delegates
.Net delegates are present as a reference type, and all delegates
inherit from the System.Delegate type.
Hence, technically, our prior definition that said 'a delegate is a
reference to a method' is not quite appropriate.
A delegate is a reference type derived from System.Delegate and
its instances can be used to call methods with matching
signatures.
Another important thing to note here is that since defining a
delegate means creating a new sub-type of System.Delegate, the
delegates can not be defined within a method (which is also true
for ordinary types).
A delegate in C# is similar to a function pointer in C or C++.
Using a delegate allows the programmer to encapsulate a
reference to a method inside a delegate object.
An interesting and useful property of a delegate is that it does not
know or care about the class of the object that it references.Any
object will do; all that matters is that the method's argument
types and return type should match the delegate's.

Signature of Simple Delegate


delegate result-type identifier ([parameters]); where
result-type: The result type, which matches the return type of the
function.
identifier: The delegate name.
parameters: The Parameters, that the function takes.
Examples:
public delegate void SimpleDelegate ()
This declaration defines a delegate named SimpleDelegate, which
will encapsulate any method that takes no parameters and returns
no value.
public delegate int ButtonClickHandler(object obj1, object obj2)
This declaration defines a delegate named ButtonClickHandler,
which will encapsulate any method that take two objects as
parameters and returns an int.

Signature of Simple Delegate (Contd.)


The syntax is similar to that for a method definition, except that
there is no method body and the definition is prefixed with the
keyword delegate.
Depending on how visible you want your definition to be, you can
apply any of the normal access modifiers to delegate definitions public, private, protected, and so on.
The crucial point to understand about delegates is that they are
very type-safe.
When you define the delegate, you have to give full details of the
signature and the return type of the method that it is going to
represent.
There are three steps in defining and using delegates:
Declaration
Instantiation
Invocation

Example
// Declaration
public delegate void SimpleDelegate();
class Program
{
public static void MyFunc()
{
Console.WriteLine("I was called by delegate ...");
}
public static void Main()
{
// Instantiation
SimpleDelegate simpleDelegate = new
SimpleDelegate(MyFunc);

// Invocation
simpleDelegate();
Console.ReadLine();

Passing Delegates as parameter


public class MyClass
{
public delegate void LogHandler(string message);
public void Process(LogHandler logHandler)
{
if (logHandler != null)
{
logHandler("Process() begin");
}
if (logHandler != null)
{
logHandler("Process() end");
}
}
}

Multi Cast Delegates


A special feature of delegates is that a single delegate can
encapsulate more than one method of a matching signature.
These kind of delegates are called 'Multicast Delegates'.
Internally, multicast delegates are sub-types of
System.MulticastDelegate, which itself is a subclass of
System.Delegate.
The most important point to remember about multicast delegates
is that "The return type of a multicast delegate type must be
void".
The reason for this limitation is that a multicast delegate may
have multiple methods in its invocation list. Since a single
delegate (or method) invocation can return only a single value, a
multicast delegate type must have the void return type.
Implementing a Multicast Delegate
A multicast delegate is defined in exactly the same way as a
simple delegates, with the exception that the return type of a
multicast delegate is strictly void.

delegate void MyMulticastDelegate(int p, int q);


The different methods are added to multicast delegate's
invocation list by using '+=' assignment operator, like this:
arithMethod = new MyMulticastDelegate(Add);
arithMethod += new MyMulticastDelegate(Subtract);
arithMethod += new MyMulticastDelegate(Max);
The invocation of a multicast delegate is again similar to
that of normal delegates and methods except that it in turn
calls all the encapsulated methods.
arithMethod(3, 4);

Events
An Event is the way for a class to provide notifications to clients of
that class when some interesting things happen to an object
The most familiar use for events is in GUI. The class that
represent controls in the interface have events that are notified
when does something to the control (E.g. Click a Button)
An event is the out come of an action
There are two important terms with respect to the events are
Event Source and Event Receiver
The object that raises the event is called Event Source
The object that responds to the event is called event receiver
The communication channel between an Event Source and An
Event Receiver is Delegate
Conventions
Event Handlers in the .NET Framework return void and take two
parameters.
The first parameter is the source of the event; that is the
publishing object
The second parameter is an object derived from EventArgs

Events are properties of the class publishing the event.


The keyword event controls how the event property is accessed by
the subscribing classes.
Class Providing Event
Method

Raise Event

Invoke/Call

Method
Class Receiving Event

Event

Delegate
Invoke/Call

Delegate Target
Method

THANK YOU

Das könnte Ihnen auch gefallen