First of all, I need to declare that the word "syntactic sugar" is by no means a derogatory term. It can bring convenience to me. It is a convenient way of writing. The compiler will help us do the conversion; it can also improve the efficiency of development and coding, in terms of performance. There will be no loss. This makes Java developers envious, haha.
1. Simplified Property
Earlier we declared the Property like this
view sourceprint?01 private string _myName;
02
03 public string MyName
04
05 {
06
07 get { return _myName; }
08
09 set { _myName = value; }
10
11 }
It doesn’t make much sense to make a cookie-cutter statement like this, so the designers of C# handed over this cookie-cutter work to the compiler to do it for us. Now we can declare it like this
view sourceprint?1 public string MyName { get; set; }
Of course, it will not sacrifice flexibility. We can set access restrictions for get or set separately, for example
view sourceprint?1 public string MyName { get; protected internal set; }
2. Commission writing method after two changes
In .net 1.1, we had to declare the method before using it in the delegate. After .net 2.0, we can use anonymous delegates, which not only simplifies writing, but also allows you to access variables within the scope in anonymous delegates; later, Ram Now that expressions are available, writing becomes easier.
view sourceprint?01 class MyClass
02 {
03 public delegate void DoSomething(int a);
04
05 //Define method delegation
06 private void DoIt(int a) {
07 Console.WriteLine(a);
08 }
09
10 private void HowtoDo(DoSomething doMethod,int a) {
11 doMethod(a);
12}
13
14 public static void Main(string[] args) {
15 MyClass mc = new MyClass();
16 //Call the defined method delegate
17 mc.HowtoDo(new DoSomething(mc.DoIt), 10);
18 int x = 10;
19 //Use anonymous delegation
20 mc.HowtoDo(delegate(int a){
21 Console.WriteLine(a + x);
22},10);
twenty three
24 //Use lambda expression
25 mc.HowtoDo(a=>Console.WriteLine(a+x),10);
26
27 Console.ReadLine();
28 }
29 }
3. Collection class declaration
Before we declared a List and assigned an initial value to the list, we had to write like this:
view sourceprint?1 List<string> list = new List<string>();
2 list.Add("a一");
3 list.Add("b二");
4 list.Add("c三");
No need now, just write it
view sourceprint?1 List<string> list = new List<string> {
2 "def","OK"
3};
4. Operations on each item of the collection class
In order to process the items in the collection one by one, we need to write like this:
view sourceprint?1 foreach (string item in list)
2 {
3 Console.WriteLine(item);
4}
It's not necessary now, that's fine
view sourceprint?1 list.ForEach(a => Console.WriteLine(a));
The code is much cleaner.
5. using == try finally
In order to release resources when finished using them, we often use using. Using is essentially just a syntax sugar for try fiannaly. For example
view sourceprint?1 StreamWriter sw = null;
2 try
3 {
4 sw = new StreamWriter("d:abc.txt");
5 sw.WriteLine("test");
6}
7 finally {
8 if(sw!= null) sw.Dispose();
9}
The above code can be simplified to:
view sourceprint?1 using (var sw = new StreamWriter("d:abc.txt")) {
2 sw.WriteLine("test");
3}
6. cute var
There is no need to write the declared type for the meaning of var. The compiler will determine its type based on the subsequent assignment to var. Once the type of var is confirmed, it cannot be changed. It can only be used as a local variable and cannot be used as a field. Make parameter declarations.
For example:
view sourceprint?1 var writer = new StreamWriter(path);
view sourceprint?1 for(var i=0;i<100;i++){}
7. The evolution of question marks
The old question mark + colon
view sourceprint?1 var b = 3;
2 var a = b > 9?b.ToString():”0”+b;
New baby has two question marks??, which means that if the variable on the left is null, the value is the variable on the right, otherwise it is the value of the variable on the left.
view sourceprint?1 string a = null;
2 var b = a??"";
8. Syntactic sugar for type instantiation
view sourceprint?1 public class Abc
2 {
3 public int ID { get; set; }
4
5 public string Name { get; set; }
6
7 public string Url { get; set; }
8}
We did not declare a constructor for the above class, but we can instantiate it like the following
view sourceprint?1 public static void Main(string[] args) {
2 var abc = new Abc{
3ID=1,
4 Name="yukaizhao",
5 Url=" http://yukaizhao.cnblogs.com/ "
6};
7}
9. legendary extension methods
Extension methods were introduced in C# 3.5. We can add instance methods to a class without modifying the class source code. This is very meaningful. Its essence is also an implementation of syntactic sugar.
For example, we extend the String class with an IsNumber method:
view sourceprint?01 public static class StringExt {
02 static private Regex regexNumber = new Regex(" \d +");
03 static public bool IsNumber(this string input)
04 {
05 if (string.IsNullOrEmpty(input))
06 {
07 return false;
08 }
09 return regexNumber.IsMatch(input);
10}
11 }
We can call this method on the String instance
view sourceprint?1 var abc = “123”;
2 var isNumber = abs.IsNumber();
10. Use anonymous classes
view sourceprint?1 var a = new {
2 ID = 1,Name=”yukaizhao”,BlogUrl=”http://www.cnblogs.com/yukaizhao/”
3};
Anonymous classes are useful when returning query data in linq to sql or entity framework.
If you have more syntactic sugar, please share it. At the same time, I hope everyone will enjoy syntax sugar, because it can bring us convenience. Please don’t sneer at it, and there is no need to sneer at it.
http://www.cnblogs.com/yukaizhao/archive/2010/05/25/csharp-Syntactic-sugar.html