Understanding and application of C # generic LAMDA expression LINQ statements

Source: Internet
Author: User

About generics

Generics are a special type that defers the work of a specified type until the client code declares and instantiates the class or method. Generics are classes, structs, interfaces, and methods with placeholders (type parameters) that are placeholders for one or more types that are stored or used by classes, structs, interfaces, and methods. A generic collection class can use a type parameter as a placeholder for the type of the object it stores, the type parameter as the type of its field, and the parameter type of its method. A generic method can use its type parameter as the type of its return value or the type of one of its parameters.

Generic types can be used to maximize the reuse of code, protect class security, and improve performance, and are often used to create generic interfaces, generic classes, generic methods, generic events, and generic delegates. You can access methods for a specific data type by constraining the generic class.

There are six kinds of parameter type constraints for generics, and you can use different rules to impose restrictions on the type types that client code uses for type parameters when instantiating a class. See figure below for specific usage.


Generics are often compared with templates in C + +, and the connection and difference between the two, see http://col1.blog.163.com/blog/static/19097751920127604811213/.


About LAMDA Expressions

A LAMDA expression is an anonymous function that can be used to create a delegate or an expression tree type. By using the LAMDA expression, you can write a local function that can be passed as a parameter or return a value as a function call, and LAMDA expressions are especially helpful for writing LINQ queries.

Lamda expressions can be implemented either by delegate or by an expression tree type.

The method of implementing the delegate is simple and convenient, and the following focuses on the expression tree.

The expression tree provides a way to convert executable code to data. LINQ provides a simple syntax for converting code to a data structure named an expression tree.

First, add namespaces, using System.Linq.Expressions;

Some of the methods in expressions are shown in the diagram.


Expressions the specific use of the class, see Http://msdn.microsoft.com/zh-cn/library/system.linq.expressions.expression_methods (v= vs.110). aspx


About LINQ Statements

LINQ, a language-integrated query, is a set of extensions for C # that allows C # code to manipulate memory data in the same way as the query database-arrays, objects, XML, datasets, and so on.


LINQ defines approximately 40 query operators, such as Select, from, in, where, and order by (C #). Use these operators to write query statements. However, these queries can also be based on many types of data, and each data type requires a separate LINQ type, as shown above.

         LINQ is very similar to a regular SQL statement, as detailed in the http://msdn.microsoft.com/library/bb397926.aspx.

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.