LINQ study Note 3

Source: Internet
Author: User

Lambad Expression Tree:

There are two ways to compile the lamabda Expression Tree:

If the first parameter of the expression is declared as ienumberable <t> type, the compiler will compile it into the middle of Il.CodeWhen the extension method inherits from iqueryable <t>, the compiler generates an Expression Tree.

VaR keyword, anonymous type, object initialization


The VaR keyword allows us to initialize variables without knowing the variable type, such:

VaR A = new {name = 'zhang san', sex = 'mal '};

But Initialization is required.

VaR A; // compilation Error

 

Object initialization:

The traditional method is to first create an object and then initialize the object, for example

Address = new address ();
Address. Address = "105 Elm Street ";
Address. City = "Atlanta ";
Address. State = "Ga ";
Address. postalcode = "30339 ";

 

However, when it is an anonymous type, the above method will not be able to be initialized

For the anonymous type, the following initialization method is available.

VaR A = new {name = 'bar '};

 

Expansion Method:

Necessity of expansion method: 1. It is impossible to create an instance method with the same (similar) operation for each object

2. Static methods cannot meet all the conditions.

The so-called expansion method is the static method of the static class called on the instance.

 

For example:

Namespace Test
{
Public static class person
{
Public static string speak (this string words)
{
Return "this person said:" + words;
}
}
}

This is an extension method, which can be used in all string instances.

Note: 1. Reference The namespace where the expansion method you write is located.

2. Extension methods cannot be written in generic classes.

 

Priority: If you declare a tostring () Extension Method in the person class above, it cannot be called in the string instance.

Because the instance method has a higher priority than the extension method.

 

 

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.