I have always heard that the delegates and events in C # are very important, there is no personal experience, and this time by doing a WinForm two times the development of the project to really feel the Commission and the sharp point of the incident.
What is the role of events and delegates in 1.c#?
An event represents a signal that a component can be concerned with, and a delegate is an object that encapsulates a process into variables for passing and executing.
2. The relationship between them?
A delegate is a type, and an event is a member, which is equivalent to the public int. age {get;set;} In this attribute, int is the type, the age is a member, the public event EventHandler Push, the EventHandler is the delegate type, the push is the event, and he is a member.
An event is essentially a delegate, but there is a more restrictive use than a delegate: an event can only be called within the class that defines the event, and the delegate is invoked in any method (difficult to understand).
We can basically say that the event is a delegate, the delegate is a method pointer, and the invocation method is the same when the delegate is invoked. In general, this restriction is used exactly when encapsulating component classes to embody the encapsulation characteristics.
3. And what is the use of delegates and events?
Events are abstractions in components, as well as States and operations, which are attributes and methods that correspond to C #. So components like controls will have events. The delegate represents the execution of a code, which is used very often after c#3.0 introduces LINQ.
4. But what are the benefits of invoking the event in the same way?
An event is a wrapper, as if a property encapsulates a field, you can isolate the definition and implementation, and give you an example of the fact that you see so many attributes, in fact, there is only one field storage time, all kinds of dates ah Ah years property is calculated according to this time. Events also encapsulate the fields of the internal type as a delegate so that only events can be used to register or unregister event attention outside of the class, not to raise events.
[C #] Delegate instance analysis