C # icing Code

Source: Internet
Author: User

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

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.