About .Net Events

A .Net event actually consists of a pair of accessor methods named add_EventName and remove_EventName.  These functions each take a handler delegate, and are expected to add or remove that delegate from the list of event handlers. 

In C#, writing public event EventHandler EventName; creates a field-like event.  The compiler will automatically generate a private backing field (also a delegate), along with thread-safe accessor methods that add and remove handlers from the backing field (like an auto-implemented property).  Within the class that declared the event, EventName refers to this private backing field.  Thus, writing EventName(...) in the class calls this field and raises the event (if no handlers have been added, the field will be null).

You can also write custom event accessors to gain full control over how handlers are added to your events.   For example, this event will store and trigger handlers in reverse order:

void Main()
{
    ReversedEvent += delegate { Console.WriteLine(1); };
    ReversedEvent += delegate { Console.WriteLine(2); };
    ReversedEvent += delegate { Console.WriteLine(3); };

    OnReversedEvent();
}

protected void OnReversedEvent() {
    if (reversedEvent != null)
        reversedEvent(this, EventArgs.Empty);
}

private EventHandler reversedEvent;
public event EventHandler ReversedEvent {
    add {
        reversedEvent = value + reversedEvent;
    }
    remove {
        reversedEvent -= value;
    }
}

This add accessor uses the non-commutative delegate addition operator to prepend each new handler to the delegate field containing the existing handlers.  The raiser method simply calls the combined delegate in the private field. (which is null if there aren’t any handlers)

Note that this code is not thread-safe.  If two threads add a handler at the same time, both of them will read the original storage field, add their respective handlers to create a new delegate instance, then write this new delegate back to the field.  The thread that writes back to the field last will overwrite the changes made by the other thread, since it never saw the other thread’s handler (this is the same reason that x += y is not thread-safe).  The accessors generated by the compiler are threadsafe, either by using lock(this) (C# 3 or earlier) or a lock-free threadsafe implementation (C# 4).  For more details, see this series of blog posts.

This example is rather useless.  However, there are better reasons to create custom event accessors. WinForms controls store their events in a special EventHandlerList class to save memory.  WPF controls create events using the Routed Event system, and store handlers in special storage in DependencyObject.  Custom event accessors can also be used to perform validation or logging.

3 comments:

The above article is nice and interesting, thank you willing to share! Greetings success of admin Percetakan Murah Rawamangun Jakarta Timur wish you deign to visit my website, thank you :)

Thank you so much or your great post, very useful post. I find reading this article a joy. It is very helpful and interesting and I am really looking forward to reading more of your work. I feel so good to become visiting your weblog again. It has been months for me. I do not read the blog daily. I occasionally do read blogs.
ASP.NET is a web application framework formed by Microsoft to let programmers make dynamic web sites. It lets you utilize a full-featured programming language like C# or VB.NET to build web applications simply. This tutorial covers all the vital elements of ASP.NET that a beginner would need to get started. ASP.NET is a web development platform. It is a complete software infrastructure and a selection of services vital to make up robust web applications for PC, as well as mobile devices. ASP.NET workings on top of the HTTP protocol, and relate the HTTP commands and policy to put a browser-to-server joint communication and cooperation. ASP.NET is a piece of Microsoft.Net platform. ASP.NET applications are compiled codes, written by the extensible and reusable components or objects herein.Net framework. These codes can apply the entire hierarchy of the class’s in.Net framework. SP.NET web forms expand the event-driven model of interaction to web applications. The browser submits a web form to the web server and the server precedes a full markup page or HTML page in reply.
There are a lot of online essay writing services in the world. I think the best service is a custom essay writing service with the best guidelines. You can use it for the better. I expect you will be 100% pleased with their services. A few months ago I used this and usually write a personal narrative essay when I face hard in writing. Hope you will also be helped by them.

You should definitely take a look at these advices if you want to get into Harvard. It will help you to achieve success

Post a Comment