It's easier to explain delegates first.
Delegates
A delegate example
Define the delegate type, or use one of the many predefined types shipped with the FCL.
public delegate void Function(string value);
Define the target methods that we can call. These will have to conform to the signature of the chosen delegate type.
static void WriteOut(string value)
{
Console.Out.WriteLine(value);
}
static void WriteError(object value)
{
Console.Error.WriteLine(value);
}
Define a class with a public delegate field (bad object-oriented encapsulation, but it's just for the purpose of example).
class DF
{
public Function function;
}
Construct a new instance of this class, add a couple of target methods and invoke them through the delegate:
DF df = new DF();
df.function += new Function(WriteOut);
df.function += new Function(WriteError);
df.function("Delegate");
Events
An event is a construct that is functionally similar to a delegate, but provides more encapsulation. Specifically, adding/removing target methods is given the accessibility of the event (e.g. public/protected/internal). Everything else is given the accessibility modifier of "private". This allows subscribers to be notified of an event, but not to trigger it themselves.
An event example
Again, pick a delegate type from the FCL or define your own. For this example, I will reuse the Function delegate type, and the two Function methods defined above.
Define a class with an Event:
class EF
{
public event Function function;
}
Construct a new instance of this class, add a couple of target methods:
EF ef = new EF();
ef.function += new Function(WriteOut);
ef.function += new Function(WriteError);
Because of the encapsulation, we cannot call the following method from outside the declaring type:
//ef.function("Event"); // ... can only appear on the left hand side of += or -= (unless used from within the event's declaring type)
A good technical resource:
Difference between a delegate and an event.
No comments:
Post a Comment