How does a delegate eventually become a LINQ expression? (I)

Source: Internet
Author: User

Not long ago, I gave the students training about C # Language delegation. It took me one day. The delegate is introduced to the LINQ expression. However! The students did not respond very well. It seems that you have not thoroughly understood the process. I wrote this article. This is a summary for the students!

  1. What is delegation?

In fact, we can understand the delegate as an object that contains one or more methods. Most of the time we use a delegate, we use the "execute" object. However, the delegate is different from the general object. We need to execute the methods it owns.

The following is an example code.

Using system; using system. collections. generic; using system. LINQ; using system. text; namespace delegate {delegate void myprint (INT value); // defines the delegate class program {void chineseprint (INT value) {console. writeline ("Chinese land and area {0}", value) ;}// two methods with the same parameters as the delegate return value void japaneseprint (INT value) {console. writeline ("Japanese land and area {0}", value);} static void main (string [] ARGs) {program P = new program (); // declare the current class console. writeline ("enter a country"); string Country = console. readline (); If (country. equals ("Japan") {myprint printdelegate = new myprint (P. chineseprint); printdelegate (1235);} else {myprint printdelegate = new myprint (P. chineseprint); printdelegate (9600000 );}}}}
Simple delegate Definition

Let's take a look at the comparison between the delegate and the class. The delegate is also a user-defined type, but the class represents a set of encapsulated data and methods, the delegate holds one or more methods and a series of custom operations.
①: Declaring a delegate type is similar to the method life, but the delegate is not implemented.

②: Use This delegate type to declare a delegate variable.

③: Create a delegate type object and assign it to the delegate variable. The new delegate object contains references to a method. The signature of this method must be consistent with the return value.

④: You can add other methods to the delegate object.

⑤: Calling a delegate in code can be like calling a method. All the methods are executed.

We can regard delegate as an object that contains the list of ordered methods. These methods have the same signature and return type. The delegate save method can come from any class and structure.

How to declare a delegate type

Delegate (keyword) void (return type) mydelgate (Signature) (int x) (parameter)

The delegate type declaration looks very similar to the method declaration, including the return type and signature. The return type and signature specify the Receiving Method of the delegate method.

①: Starting with the delegate keyword;

②: No method subject.

Create delegate object

A delegate is a reference type, so there are references and objects. After the delegate type declaration, we can declare variables and create type objects. The following code demonstrates the variable Declaration of the delegate type:

MYDEL delvar

(Delegate type) (variable)

There are two ways to create a delegate object. The first is an object expression using the new operator, as shown below:

①: Delegate type name

②: A group of parentheses that contain the name of the method used as the first member in the call list. The method can be an instance method or a static method.

Using system; using system. collections. generic; using system. LINQ; using system. text; namespace consoleapplication1 {Public Delegate int mydelegate (string name); Class program {static void main (string [] ARGs) {mydelegate mydel1 = new mydelegate (max ); mydelegate mydel2 = new mydelegate (min); mydelegate MYDEL = max; // You can also use the shortcut syntax to directly assign the method name to MYDEL} static int max (string I) {return Int. parse (I);} static int min (string I) {return Int. parse (I );}}}

The following code creates two delegate objects, one with the instance method and the other with the static method.

 

How does a delegate eventually become a LINQ expression? (I)

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.