1: Implicit local variables and Arrays
2: Object Initial Value Setting item
3: Set the initial value.
4: Automatically implement attributes
5: Anonymous type
6: Extension Method
7: Division method definition
8: Lambda expression
Keywords(Var)
1: var can be declared with local variables. fields that cannot be used can be applied to for, foreach, and using statements.
2: The var keyword cannot contain its own object or set initiator, but it can pass through a new expression.
For example, var result; // compilation Error
Var result = result + 1; // compilation Error
Var result = {1, 2, 3}; // compilation Error
3: var is an inference type, not a display type
4: The var keyword indicates that the compiler can deduce the variable type based on the expression on the right of the initialization statement.
5. the inference type can be a built-in type, an anonymous type, a user-defined type, or a type or expression defined in the. NET Framework class library.
Note: The var keyword does not mean "variant", nor does it mean that variables are loosely typed or later bound to the variable. It only indicates that the compiler determines and assigns the most suitable type.
VarUse Cases
1: local variables
For example, var I = 5;
2: In the for initialization statement
Example: for (var I = 1; I <10; ++ I)
3: Initialize the statement in foreach
Example: foreach (var item in list)
4: using statement
Example: using (var file = new StreamReader ("C :"))
1. Implicit local variables and Arrays
Note: The Invisible array is created using the var keyword and array initiator. The data type of the element must be implicitly converted to the same data type and cannot be null)
1: square brackets are not used for arrays of the implicit type on the left of the initialization statement.
2: Support for staggered arrays, not multi-dimensional arrays
For example, var a = new [] {1, 2, 3} // One-dimensional array
Var B = new []
{
New [] {1, 2, 3 },
New [] {5, 6}
}; // Staggered Array
Ii. Object initial value settings
Note: The object initializer is composed of a series of member objects, whose objects must be initialized. They must be separated by commas (,) and closed {}.
1. NET 2.0 Statement:
User userInfo = new User ();
UserInfo. ID = "zhuxing ";
UserInfo. Name = "czx ";
UserInfo. Age = 22;
2. NET 3.5 statement:
User userInfo = new User () {ID = "zhuxing", Name = "czx", Age = 22 };
Note: Nested complex attribute types
User userInfo = new User ()
{
ID = "zhuxing ",
Name = "czx ",
Address = new Address ()
{
Province =" Fuji ",
City = "ningde"
}
};
1: it can be used together with the constructor, And the constructor Initialization is executed before the object initializer.
2: partial assignment is allowed.
3: allow assignment to internal members
Public class user
{
Public String Name {get; set ;}
Public String Age {get; set ;}
Private Int32 test = 25;
Internal Int32 test2;
}
Public class Program
{
Static void Main (String [] args)
{
User person = new user {Name = "Michael", Age = "male", test2 = 20 };
Console. WriteLine ("{0}, {1}, {2}", person. Name, person. Age, person. test2 );
Console. ReadLine ();
}
}
// Used with the constructor
Public class user
{
Public String Name {get; set ;}
Public Int32 Age {get; set ;}
Private Int32 test = 25;
Internal Int32 test2;
Public user (Int32 Age)
{
This. Age = Age;
}
}
Public class Program
{
Static void Main (String [] args)
{
User person = new user (20) {Name = "Zhang San", Age = 22, test2 = 20 };
Console. WriteLine ("{0}, {1}, {2}", person. Name, person. Age, person. test2 );
Console. ReadLine ();
}
}
Iii. set initial values
Note:
1: The set initiator is composed of a series of set objects, separated by commas (,) and closed.
2: The set initialization tool calls the ICollection <T>. Add (T) method in sequence for the elements in the initialization tool.
Example: List <int> number = new List <int> {1, 2, 3, 4, 5 };
// Set Initiator
Public class parame
{
Public String Name {get; set ;}
Public Int32 Age {get; set ;}
}
Public class Program
{
Static void Main (String [] args)
{
IList <parame> people = new List <parame> ()
{
&