Delegate, which once made me hard to understand

Source: Internet
Author: User

When I first started to learn C #, I read the basic syntax and something. I wrote a demo and understood it. When I went back to use it, I started to use it. I had read a lot about this delegation, I never know when and where he will play a role! Remember only one keyword --"Delegate"And concept-"Is a reference type for methods with specific parameter lists and return types.".

It's hard to crack !!!!!!!!!!

I think about it recently. I want to write down my new understanding .....

Basic

Keywords: Delegate

Statement Syntax:

Public Delegate return value type delegate name (parameter list );

Instantiation: Delegate name Instance name;

Personal Understanding: "delegation" is entrusted to others as its name implies. In a language environment, it is entrusted to an object. To put it bluntly, authorization is used. I gave the bank card to a and told him my password. I entrusted him to help me get the money (of course, I trust it very much, otherwise there is a risk ), A has the right to get the money from my bank card.

In the program, it is a truth.

 

Instance demo

Demonstration purpose: Use a delegate to produce letters. Obtain the existing value from Textbox, specify the delegate binding method, and add it to ListBox.

Open vs2013 to create a winform project.

Create a new capital class, create only one method in the class, and convert the string to uppercase letters. The Code is as follows:

 public class Capital    {        public string fixText(string s)        {            return s.ToUpper();        }    }

Create a lower class, create only one method in the class, and convert the string to a letter. The Code is as follows:

 public class Lower    {        public static string fixText(string s)        {            return s.ToLower();        }    }

Life delegate and instantiate

  private delegate string ftxDelegate(string s);        ftxDelegate ftx;

 

Some code is omitted. The execution result is as follows.



The complete DEMO code is pushed here.

Access Password 799c

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.