c#2.0 generic Proxies and events: With a thrill of the Hundred

Source: Internet
Author: User
Tags define definition
Today, using c#2.0 's generics to rewrite the message implementation of one of my systems, it feels very good, simple and fast (as MSDN says, hint-inducing makes me feel this way too, hehe).

Let's do some chatter and review the classics:
. NET Framework, built on a proxy that connects the event handle ( Delegate, to trigger an event, two basic elements must be considered: 1 , the source trigger for the event, which defines an event:public class Eventsender
{     ...
Declares a proxy type, in this case, it points to a method without a return value and has two parameters
public delegate void MyEventHandler (obejct sender, EventArgs e);
Instantiate an event member by the agent
public event MyEventHandler MyEvent;
...
In a place based on logic triggers (Fire) This event
To inform the system: "Boss you listen, I sent an incident."
This.myevent (This,null);
...
} 2 , the recipient of the event , It registers the eventpublic class Eventreceiver
{...//instantiate the event emit class above
Eventsender sender = new Eventsender ();
Register the Eventsender MyEvent event and tell the system: "If this object sends out the event, I would be happy to accept"
At the same time inform the system: "Boss, I have the ability and the urgent need to deal with this event through my Onreceivedtheevent method"
Sender.myevent + = new MyEventHandler (onreceivedtheevent); ... private void Onreceivedtheevent (object sender, EventArgs e)
{
To achieve their own processing
}
Of course, if we don't need to pass the custom (inherited) EventArgs, we can use a message agent that the system has already defined EventHandlerInstantiate an event member directly, eliminating the steps to define the proxy type. In this case, the purpose of the triggering event is simply to act as a notification and not to pass run-time data (because System.EventArgsis the most basic event parameter class, and the other event argument classes inherit the class, such as MouseEventArgsis not capable of accommodating user-defined information, such as if you want to pass a state value of an object while triggering an event ... System.EventArgs powerless.

The above is the classical event processing mode, in the c#2.0, event processing introduced the generic mechanism, thus greatly convenient our programming. With this package, EventHandlerhas a generic version of it eventhandler<t>。 Below we use two versions of Code snippets to compare generics to our visual impact and programmatic enjoyment:--) tasks: Define and register three events, using three separate EventArgs inherited Objects to pass different data c#1.1 .....
Definition (in the hypothetical class MyClass)
public delegate void MyEventHandler1 (object sender, MyEventArgs1 myEventArgs1);
public delegate void MyEventHandler2 (object sender, MyEventArgs2 myEventArgs2);
public delegate void MyEventHandler3 (object sender, MyEventArgs3 myEventArgs3);
public event MyEventHandler1 Event1;
public event MyEventHandler2 Event2;
Public event MyEventHandler3 event3;......//registration (instantiation MyClass assumed)
Myclass.event1 + = new EventHandler (
THIS,MYEVENTARGS1);
Myclass.event2 + = new EventHandler (
this, MYEVENTARGS1);
Myclass.event3 + = new EventHandler (
this, MYEVENTARGS1); C # 2.0 .....
Definition (in the hypothetical class Mygoodclass)
Public event eventhandler< myeventargs1> Event 1;
Public event eventhandler< myeventargs2> event 2;
Public event eventhandler< myeventargs3> event 3;......//registration (instantiation Mygoodclass assumed)
Mygoodclass.event1 + = new eventhandler< myeventargs1> (THIS,MYEVENTARGS1);
Mygoodclass.event2 + = new eventhandler< myeventargs2> (THIS,MYEVENTARGS2);
Mygoodclass.event3 + = new eventhandler< myeventargs3> (THIS,MYEVENTARGS3); Comparison can be found that the code is not a lot of simplicity? In fact, the advantages and uses of generics are much more than that. One of the great benefits of this is type-safety and thread-safe, which used to be the object type when we needed to pass generic data, although this approach was technically feasible, but the system's overhead on boxing and unboxing and the risk of type conversions (InvalidCastException) But there are quite a few drawbacks. MSDN describes generics like this: generic classes and generic methods are both reusable, type-safe, and efficient, which are not available to Non-generic and Non-generic methods. Generics are typically used in collections and in methods that run on collections. . NET Framework 2.0 version Class library to provide a new namespace System.Collections.Generic , which contains several new generic-based collection classes. Recommended for 2.0 version of all applications uses the new generic collection class, rather than using the old Non-generic collection class, such as ArrayList . For more information, see generics in the. NET Framework Class Library (C # Programming Guide).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.