1. A simplified property
Earlier we declared the property
Copy Code code as follows:
private string _myname;
public string MyName
{
get {return _myname;}
set {_myname = value;}
}
The same statement, not much meaning, so the C # designers to the same kind of work to the compiler to help us do, we can now declare
Copy Code code as follows:
public string MyName {get; set;}
Of course, he's not sacrificing flexibility, we can set access restrictions individually to get or set, for example
Copy Code code as follows:
public string MyName {get; protected internal set;}
2. After two times of variation of the commissioned writing
At. NET 1.1 We had to declare the method before it was used in the delegate, and after. NET 2.0 we could use anonymous delegates, not only to simplify the writing, but also to access variables within the scope of anonymous delegates, and then the lambda expression, which is easier to do.
Copy Code code as follows:
Class MyClass
{
public delegate void DoSomething (int a);
To define a method delegate
private void DoIt (int a) {
Console.WriteLine (a);
}
private void Howtodo (DoSomething domethod,int a) {
Domethod (a);
}
public static void Main (string[] args) {
MyClass mc = new MyClass ();
Calling the defined method delegate
Mc. Howtodo (New DoSomething (MC). DoIt), 10);
int x = 10;
Using anonymous delegates
Mc. Howtodo (delegate (int a) {
Console.WriteLine (a + x);
},10);
Using LAMDA expressions
Mc. Howtodo (A=>console.writeline (a+x), 10);
Console.ReadLine ();
}
}
3. Declaration of collection Class
Before we declare a list and assign an initial value to the list, we have to write this:
list<string> list = new list<string> ();
List. ADD ("a One");
List. ADD ("b two");
List. ADD ("C III");
You don't need it now, just write it.
list<string> list = new List<string> {
"Def", "OK"
};
4. Operations for individual items of a collection class
In order to process items in a collection one by one, we need to write this:
foreach (string item in list)
{
Console.WriteLine (item);
}
We don't need it now, that's it.
List. ForEach (A => Console.WriteLine (a));
The code is not refreshing a lot.
5. using = = Try Finally
In order to release resources at the end of use, we often use using,using as a syntactic sugar in a try fiannaly. For example
StreamWriter sw = null;
Try
{
SW = new StreamWriter ("D:\abc.txt");
Sw. WriteLine ("test");
}
finally {
if (sw!= null) SW. Dispose ();
}
The above code can be simplified to:
using (var sw = new StreamWriter ("D:\abc.txt")) {
Sw. WriteLine ("test");
}
6. The lovely Var
The meaning of Var does not have to write the type of declaration, the compiler will be based on the value of Var after the evaluation of its type, the type of Var once confirmed can not be changed, it can only be used as a local variable, can not use as a field can not be used as a parameter declaration.
For example:
var writer = new StreamWriter (path);
for (Var i=0;i<100;i++) {}
7. The evolution of question marks
One of the oldest question marks + colons
var B = 3;
var a = b > 9?b.tostring (): "0" +B;
The new baby two question mark??, which indicates that the left variable, if NULL, is the variable on the right, otherwise the value of the variable on the left.
string a = null;
var B = A?? "";
8. Type-instantiated syntax sugar
public class ABC
{
public int ID {get; set;}
public string Name {get; set;}
public string Url {get; set;}
}
We do not declare a constructor for the class above, but we can instantiate it as follows
public static void Main (string[] args) {
var abc = new abc{
Id=1,
Name= "Jb51",
Url= "http://jb51.jb51.net/"
};
}
9. The legendary method of extension
The extension method is introduced in c#3.5, and we can add an instance method to the class without modifying the source code, which makes sense. Its essence is also the realization of a grammatical sugar
For example, we extend a Isnumber method to the String class:
View sourceprint?01 public static class Stringext {
static private Regex regexnumber = new Regex ("\\d+");
static public bool Isnumber (this string input)
{
if (string. IsNullOrEmpty (input))
{
return false;
}
return Regexnumber.ismatch (input);
}
}
We can call this method on a string instance.
var abc = "123";
var isnumber = abs. Isnumber ();
10. Using anonymous Classes
Copy Code code as follows:
var a = new {
ID = 1,name= "jb51", blogurl= "http://www.jb51.net/jb51/"
};
Anonymous classes are useful when returning query data in LINQ to SQL or the Entity Framework.
If you have more grammar candy, welcome to share. At the same time hope everyone enjoy the grammar candy, because he can bring us convenience, please do not sneer at it, there is no need to sneer at it.