new features of LINQ syntax

Source: Internet
Author: User

Implicitly typed Local variables

var =20;var username= "Lele"; var userlist =new [] {"A", "B", "C"};foreach (var user in userlist)    Console.WriteLine ( user);
var is a keyword that automatically infers types based on subsequent initialization statements, which are strongly typed. The initialization statement must be an expression and cannot be empty. And the type can be inferred at compile time. Once initialized, this type can only be stored. The Var declaration is limited to local variables and is not available for fields. can also be used in statements such as for,foreach,using.

Anonymous type

var data =new{username= "Lele", age= "20 '}; Console.WriteLine ("Username:{0} Age:{1}", Data.username,data.age);

Object Initializer

public class person{public    string Username{get;set;}    public int age{get;set;}    public override string ToString ()    {        return string. Format ("username": {0} Age:{1} ", This.username,this.age);}    } Person P =new Person () {username= "Lele", age= "30"}; Console.WriteLine (P.tostring ());

The compiler will automatically do the setter for you, allowing the property assignment operation to be done in a row. Here's what to note:

Allows assigning values to only a subset of attributes, including internal access levels

Can be used in conjunction with constructors, and constructor initialization is performed before the object initializer.


Collection initializer


public class person{public    string Username{get;set;}    public int age{get;set;}        public override string ToString ()    {        return string. Format ("username:{0} Age:{1}", This.username,this.age);}    } var persons =new list<person>{    new Person{username= "A", age=1},    new person{username= "B", age=2}}; foreach (var p in persons)    Console.WriteLine (p.tostring ());

The compiler will do a collection insert for you automatically. If you initialize for Hashtable, it is equivalent to using two object initializers.


Lambda expression


will be presented in detail in the next blog post.






new features of LINQ syntax

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.