LINQ (Language Integrated Query) (2): prerequisites, extension methods, Lambda

Source: Internet
Author: User

The extension method. Lambda is a new feature in C #3.0 like Linq. the Extension Method and Lambda (Lambda expression) are used much in Linq, but they can also be used independently in other places. for example, Lambda is much more used in delegation and events.

Extension Method
Extension, as its name implies, is to add something on the original basis. the effect achieved by the Division class is somewhat similar. we know that when defining a class, we can add a partial before it, then we can write the class code in several separate files. it is merged into a class during compilation. the extension method is after you define a static method associated with a class anywhere else (of course there is a little difference with the general method syntax, and it is not really anywhere, must be defined in a static class), it is equivalent to adding a method to the original class. in fact, we can also inherit a class and add a function to achieve a similar purpose. this is a little too troublesome, and some classes are sealed and cannot be inherited.

For a simple example, we know that the string class does not have the Add (string str) method, so we can get an extension method. Let the string class have this method.

Public static class ExtensionMethods

{

/* The following method is the so-called extension method. It is very similar to the general method. However, there is a this keyword in it. The class that appears after this indicates the class associated with this method.

This string str indicates that this method is associated with the string class. str cannot be considered as a function parameter in a common method. it is essential for definition. but it can be used in the function body or not. in addition, str is only a parameter name. You can name it as needed. aside from this string str, this is a bit strange. the rest is exactly the same as the general method. how many parameters can you add at will. */

Public static string Add (this string str, string addStr)

{

Return str + addStr;

// If str is not used, return addStr can also be written in this way.

}

}

With the above definition, you can use this function in any other place that uses the string class.

For example

String str = "Hello ";

String newStr = str. Add ("arwen ");

 

In fact, we do not need to use the extension method when writing code by ourselves. However, if you develop third-party controls and expand the functions of those controls seen in VS, the extension method will certainly be greatly increased.

In addition, many extension methods have been written in Linq so that we can call them. You will see many extension methods in the static function Enumerable under the namespace System. Linq.

So when you use the array after referencing the Linq namespace, you will see that there are more Any and All strange methods.

 

Lambda expressions
 

Lambda expressions are a term in mathematics. the meaning represented in C # can be understood as representing a function in a form that is somewhat like a function in mathematics. in mathematics, we can see functions like y = f (x. x indicates a variable,

Y is the return value. we used to define a function in C # Before calling it to implement such a function. but now you can use the function directly without defining it before calling. let's take a simple example.

Func <int, int, int> fun =

(A, B) => a * B;

Int one = 4;

Int two = 5;

Int y = fun (one, two );

Func is a predefined proxy. Here we bind a function to the proxy. however, we do not need to define a function in other places before using that function name to call it. directly (a, B) => a * B; it is the so-called Lambda representation. in fact, it is equivalent to a function. The form is relatively simple and can be used directly. it is actually a simpler anonymous method. we can also use the delegate keyword to represent an anonymous method. for example, the above Lambda expression is equivalent

Func <int, int, int> fun =

Delegate (int a, int B) {return a * B ;}; // This is an anonymous method.

 

Let's talk about the Lambda format:

Use => as the dividing line (an equal sign must be greater than a space in the middle). The first part is the parameter, followed by the function body. (a, B) => a * B; it is just a short form. Its complete form should be

(Int a, int B) =>{ return a * B ;};

// Generally, the parameter types in brackets can be omitted. because it can be inferred from the parameter type of the previous proxy. but if you want to explicitly write the type, all parameters must be written at the same time.

If it is written as (int a, B), it will be wrong.

 

Functions implemented through select and where keywords through extension methods and Lambda
 

String [] names = {"arwen", "james", "sunny", "lily", "ada "};

Var name = from na in names www.2cto.com

Where na. StartsWith ("")

Select na;

If such a statement exists, it is equivalent to the following statement.

Var myName = names. Where (str => str. StartsWith (""));

Author: weiwenhp

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.