Delegate (Delegate) and events (event) in. net:c#

Source: Internet
Author: User

delegates and events are widely used in the. Net framework, however, a good understanding of delegates and events is not easy for many people who are not in touch with C # for a long time. They are like a sill, after the threshold of the people, it is very easy, and no past people every time to see the Commission and events feel in the heart to panic, uncomfortable. In this article, I'll go through two examples of what is a delegate, why I use delegates, the origins of events, the meanings of delegates and events in the. Net framework, delegates, and events to the observer design pattern, and also a discussion of their intermediate code.  P1: What is a delegate? Is the event a delegate? A: A delegate can take a method as a parameter into another method. It can also be easily understood as a reference to a function. Yes, a special kind of delegate.   2. What are the actual applications of the consignment?

I'll give you two examples.

1,[3A] Greetings in different languages.

2,[3B] The Chinese version of QQ and Tencent QQ (International version of QQ) user personal Information "gender" presentation.

3, two delegate (Delegate) instance parsing.

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;/// <summary>///Language Class/// </summary>namespacegyg.de.models{ Public classLanguage {/// <summary>        ///declaring a delegate/// </summary>        /// <param name= "name" ></param>         Public Delegate stringGrateingdelegate (stringname); /// <summary>        ///Chinese Greetings/// </summary>        /// <param name= "name" ></param>        /// <returns></returns>         Public Static stringEnggrateing (stringname) {            return "English name ="+name; }        /// <summary>        ///British Greetings/// </summary>        /// <param name= "name" ></param>        /// <returns></returns>         Public Static stringChinagreateing (stringname) {            return "Chinese name ="+name; }         Public Static stringGreateperpon (stringname, Grateingdelegate grateperpon) {            returngrateperpon (name); }    }}

Case 2:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;namespacegyg.de.models{ Public classSex { Public Delegate stringSexdalegate (stringSexcode);  Public Static stringChinasex (stringSexcode) {            Switch(sexcode) { Case "0":                    return "male";  Case "1":                    return "female"; default:                    return "Unknown"; }        }         Public Static stringEngsex (stringSexcode) {            Switch(sexcode) { Case "0":                    return "Girl";  Case "1":                    return " Boy"; default:                    return "Unknown"; }        }         Public Static stringSexconvert (stringSexcode, Sexdalegate Sexconvert) {            returnSexconvert (Sexcode); }    }}

Controller code:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSYSTEM.WEB.MVC;usingGyg.DE.Models;namespacegyg.de.controllers{ Public classPerponcontroller:controller {//Get:perpon         PublicActionResult Index () {stringName = Language.greateperpon ("gu Peony", language.chinagreateing); stringNameeng = Language.greateperpon ("Gyg", language.enggrateing); viewdata["name"] =name; viewdata["Nameeng"] =Nameeng; returnView (); }         PublicActionResult sextest () {viewdata["Chinasex"] = Sex.sexconvert ("0", Sex.chinasex); viewdata["Engsex"] = Sex.sexconvert ("1", Sex.engsex); returnView (); }    }}

Page code:

@{Layout=NULL;}<! DOCTYPE html>"Viewport"Content="Width=device-width"/> <title>Index</title>@ViewData ["name"] <br/>@ViewData ["Nameeng"] <br/> <a href="/perpon/sextest">SexTest</a> </div></body>
@{Layout=NULL;}<! DOCTYPE html>"Viewport"Content="Width=device-width"/> <title>SexTest</title>@ViewData ["Chinasex"] <br/>@ViewData ["Engsex"] <br/> <a href="/perpon/index">Index</a> </div></body>

4 . What are the benefits of using a delegate (Delegate)?

We now make a summary of the Commission:

  A delegate is a class that defines a method's type so that it can be passed as a parameter to another method, which dynamically assigns the method to the parameter, avoids the large use of the If-else (Switch) statement in the program, and makes the program more extensible.

Delegate (Delegate) and events (event) in. net:c#

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.