Professional Documents
Culture Documents
Soapbox Delegates Part 2
Soapbox Delegates Part 2
Part 2
A delegate is a type that defines a method signature. When you instantiate a delegate,
you can associate its instance with any method with a compatible signature. You can
invoke (or call) the method through the delegate instance. (
https://msdn.microsoft.com/en-us/library/ms173171(v=vs.90).aspx)
publicdelegateint PerformCalculation(int x, int y);
Delegates are like C++ function pointers but are type safe.
Delegates allow methods to be passed as parameters.
Delegates can be used to define callback methods.
Delegates can be chained together; for example, multiple methods can be called on a
single event.
Basic Example
BasicDemo.cs
https://msdn.microsoft.com/en-us/library/ms173172(v=vs.90).aspx
Bookstore Example
BookDemo.cs
https://msdn.microsoft.com/en-us/library/aa288459(v=vs.71).aspx
Events
An event in C# is a way for a class to provide notifications to clients of that class when
some interesting thing happens to an object.
The most familiar use for events is in graphical user interfaces; typically, the classes
that represent controls in the interface have events that are notified when the user
does something to the control (for example, click a button).
Events provide a generally useful way for objects to signal state changes that may be
useful to clients of that object.
Events are declared using delegates.
Events Example
EventDemo1.cs
EventDemo2.cs
https://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx
References
https://msdn.microsoft.com/en-us/library/ms173171(v=vs.90).aspx
https://msdn.microsoft.com/en-us/library/ms173172(v=vs.90).aspx
https://msdn.microsoft.com/en-us/library/aa288459(v=vs.71).aspx
https://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx
http://www.codeproject.com/Articles/884981/Understanding-Delegates-in-Csharp-for-be
ginners
https://msdn.microsoft.com/en-us/library/ms173173(v=vs.90).aspx