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