Generic delegate Instance tutorial for C # Foundation

Source: Internet
Author: User

In this paper, the usage of generic delegate in C # is described, and the usage analysis is carried out in detail in the example form. Share it for everyone's reference. Specific as follows:

First, the generic delegate is a special form of the delegate, although the feeling looks rather strange, in fact, in the use of the same time as the delegate, but the generic delegate is more type-universal.

Take the example of the most common delegate EventHandler in C #. Before. NET 2.0, which was generic, the normal event handler was defined by EventHandler, as follows:

public delegate void EventHandler (object sender, EventArgs e);

EventHandler refers to such a function, which has no return value and has two parameters, the first argument is the object type, and the second argument is the EventArgs type.

For. NET 2.0 and later versions, because of the introduction of generics, some built-in (built-in) classes, interfaces, and delegates have their own generic versions. EventHandler is no exception, it has its own generic version: Eventhandler<t>, which is defined as follows:

[Serializable]public delegate void Eventhandler<teventargs> (object sender, Teventargs e) where Teventargs: EventArgs;

You should be able to see that the type of the second parameter is changed from EventArgs to Teventargs, and Teventargs is what the caller decides. Assuming that both Inteventargs and Stringeventargs inherit from System.EventArgs, then:

1.eventhandler<inteventargs> refers to such a function: These functions do not return a value, have two arguments, the first argument is the object type, the second argument is the Inteventargs type

2.eventhandler<stringeventargs> refers to such a function: These functions do not return a value, have two arguments, the first argument is the object type, the second argument is the Stringeventargs type

In fact, eventhandler<inteventargs> and eventhandler<stringeventargs> are two completely different delegates, the functions they refer to have different signature forms respectively. See the following example:

Class inteventargs:system.eventargs{public  int Intvalue {get; set;}  Public Inteventargs () {} public  Inteventargs (int value)  {this. Intvalue = value; }}class stringeventargs:system.eventargs{Public  string StringValue {get; set;}  Public Stringeventargs () {} public  Stringeventargs (string value)  {this. StringValue = value; }}class program{  static void Printint (object sender, Inteventargs e)  {    Console.WriteLine (e.intvalue);  }  static void Printstring (object sender, Stringeventargs e)  {    Console.WriteLine (e.stringvalue);  }  static void Main (string[] args)  {    eventhandler<inteventargs> ihandler = new eventhandler< Inteventargs> (printint);    eventhandler<stringeventargs> Shandler = new eventhandler<stringeventargs> (PrintString);    Ihandler (NULL, New Inteventargs ());    Shandler (NULL, New Stringeventargs ("Hello World"));}  }

The specific characteristics of generics and its application in object-oriented thinking, the site has a detailed interpretation of the relevant articles, interested readers can refer to the reference.

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
Generic delegate Instance tutorial for C # Foundation

This address: http://www.paobuke.com/develop/c-develop/pbk23542.html






Related content C # using the dir command to implement a file search feature example C # multi-Threading vs. asynchronous the way C + + pointers use parameters to change arguments C # dynamic object (dynamic) details (Implementation methods and properties)
C # using GDI to draw common graphics and text to implement a multi-window interface with left and right layouts using C # code to get stored procedure return Values example analysis of the sort function usages of ArrayList in C # WinForm

Generic delegate Instance tutorial for C # Foundation

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.