Mastery of C # 12th Linq

Source: Internet
Author: User

LINQ was first introduced after the. Net3.5, the query language is easy to learn, the usable range is very wide learning before, has been used in database operations, but after learning this lesson only found that any implementation of the generic interface type can use LINQ, simply to implement IEnumerable <T> interface. So what is generics? What is non-generic?

First talk about non-generics, yes. Net2.0 has been used in the collection class before, it is characterized in that it does not need to specify the type of data when storing data, even in a collection can store many different types of data, because these container classes are accepted System.Object, that is, as long as the basic and object data, non-generic container class can be Storage, but the compiler will store the data at the bottom of the system automatically boxing operations, when extracting data and we need to manually do the unboxing, which increases the chances of code errors. The CLR (the CLR is the common language runtime (Common Language Runtime) and a Java Virtual machine is also a runtime environment that is responsible for resource management (memory allocation and garbage collection, etc.) and guarantees the necessary separation between the application and the underlying operating system. The CLR has two different translation names: the common language runtime and the common language runtime. A large number of memory conversion operations are performed during packing and unpacking operations, which slows down the run time, and it's important to mention that these classic classes are not type-safe and are themselves developed for manipulating the object class, so using these classes can result in low-performance code, Especially when manipulating array data. Next, the generic container class, which is the most important difference from non-generics is type-safe, when defining a generic, you need to define the data type of the data that the generic holds, and if the data is added and the definition is inconsistent, the compiler will error. Because the data type is already defined, you do not need a boxing unboxing operation when you use it.

Rotary linq,linq can be written in three ways, the first of which is to use the C # operator directly, in the form of: var result=from item in container where Booleanexpression Select item The second type is var result=container.where (o=> Filtering of the data in the container). Select item; The third is the use of enumerable types and anonymous methods, for example:

static void Main ()

{

String[] str={"11", "22", "33"};

Func<string,bool> search=delegate (string game) {return game. Contains ("");};

Func<string,string> items=delegate (string s) {return s;};

var result=str.where (search). Select (items);

}

In short, LINQ is a handy language, and he can be a lot less code when it comes to manipulating data, saving a lot of time.

Mastery of C # 12th Linq

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.