You will get many example on how to use delegates and events.In this post,I`ll try to explain about delegates and event concept in C#
In the above example The client code(any application) consumes service code(it can be any code/application which provide some kind of processing/service to the client) by object construction or any other means.The client code can call methods from the service code based on the domain context but how does service code can call methods from the client code .Essentially,They are Callback requirements.In C#, We have "Delegate" and "event" concept to implement this kinds of callbacks
With Delegate, The client code will get full flexibility of invoking itself and Event provide a limited flexibility to the client code.
Key Points:
- This client code is called subscriber to this service code
- There can be many subscribers
- The methods(in client code) which are to be called from service code are called Callbacks/Event Handler
- It is common practice to use private delegate or instead use Events
- There can be many callbacks withing same client code or different client code
- In C# , delegates are all type safe , It means that, All subscriber should provide callbacks which has same signatures with that of delegate defined on the service side