1 Automatic Properties
public class Student
{
public int id{get;set;}
}
2 implicit type (type inference)
The keyword VAR (the type of the variable is determined at compile time, and the inferred type is replaced with Var at compile time.) is not a weak type)
Role: Just handy programmer
var i=5;//int
Attention:
Type that cannot be a member of a class
cannot be a parameter of a method
Cannot be a return value
(can only be inferred as a type of local variable)
3 Anonymous Types
var p1=new {id=1,name= "Coco", age=22}; The members in the anonymous class are read-only seal types
Array
var intarray=new []{2,3,4,5};
var atrarray=new []{"HELLP", "Coco"};
var objarray=new[]{new {name= "Coco", age=12},new {name= "Bingbing", age=95}};
var a=intarray[0];
4
Object vs. Collection initializer
Student stu=new Student () {id=1,name= "Coco", age=99}//more convenient to assign values to objects
Collection initializer
List<int>num=new list<int> () {0,1,23,4};
List<student>stus=new list<student> () {new student{id=1,name= "Coco", Age=123},new student{Id=2,Name= " KKK ", age=17}};
5 extension methods (many in LINQ)
The extension method is mainly to write another static method in the class, the user can directly point out without changing the class. Generally not used
To declare an extension method:
The class must be static, the method is static, the first parameter is an extended object, and the previous callout is this (the This parameter type parameter name).
The extension method must be used to ensure that the extension method is already in the current code (that is, to have the same namespace)
Extension methods do not belong to members of the extension class and do not have access to private variables of the class to be extended
eg
Defined
public static Class Stingext
{
Pulic static bool Isemail (this string str)
{
return Regex.IsMatch (str,@ "^\[email protected]\w+\.\w+$");
}
}
Use
String email= "[email protected]"
Cw{emial. Isemail ();}
C # icing Code