C# 6.0新特性

C# 6.0新特性

微软在Visual Studio 2015中更新C#语言到6.0,添加了很多很好的特性,以使C#语言继续跻身于最优秀语言之行列。下面通过一个例子快速感受一下C# 6.0的新特性,以下程序在VS2015预览版中测试通过,正式版可能还要添加新特性。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Console;
 
namespace CSharp6Research
{
    //分数
    public class Fraction
    {
        public int A { get; set; }
 
        public int B { get; set; } = 1;
 
        public string Separator { get; } = "/";
 
        public string SeparatorSpaces { get; } = string.Empty;
 
        public double Value => (double)A / B;
 
        public int this[int index] => index == 0 ? A : B;
 
        public int this[string index] => index == "A" ? A : B;
 
        public override string ToString() => "{A}{SeparatorSpaces}{Separator}{SeparatorSpaces}{B}";
 
        public void Print() => WriteLine(ToString());
 
        public Fraction()
        {
 
        }
 
        public Fraction(int a, int b)
        {
            A = a;
            B = b;
        }
 
        public Fraction(int a, int b, string separatorSpaces) : this(a, b)
        {
            SeparatorSpaces = separatorSpaces;
            if (string.IsNullOrEmpty(separatorSpaces))
            {
                throw new ArgumentNullException(nameof(separatorSpaces));
            }
        }
 
        public static readonly Dictionary<string, Fraction> CommonFractions =
            new Dictionary<string, Fraction>
            {
                ["zero"] = new Fraction(),
                ["one"] = new Fraction(1, 1),
                ["half"] = new Fraction(1, 2),
                ["quarter"] = new Fraction(1, 4),
                ["infinity"] = new Fraction(1, 0),
            };
 
    }
 
    public struct FractionStruct
    {
        public int A { get; }
        public int B { get; }
        public FractionStruct(int a, int b) { A = a; B = b; }
        public FractionStruct() : this(0, 1) { }
 
        public override string ToString() => "{A}/{B}";
 
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            foreach (var f in Fraction.CommonFractions)
            {
                WriteLine("{f.Key} : {f.Value.Value}");
            }
 
            var fraction = new Fraction(1, 3, " ");
            fraction.Print();
 
            try
            {
                fraction = new Fraction(1, 2, null);
            }
            catch (ArgumentNullException e) if (e.ParamName == "separatorSpaces")
            {
                WriteLine("separatorSpaces can not be null");
            }
 
            Fraction v;
            Fraction.CommonFractions.TryGetValue("harf", out v);
            v?.Print();
            var a = v?.A;
            WriteLine(a == null);
            var b = v?["B"];
            WriteLine(b == null);
            WriteLine(v?.ToString() == null);
 
            WriteLine(new FractionStruct().ToString());
            WriteLine(default(FractionStruct).ToString());
        }
 
    }
}

运行结果如下,

1
2
3
4
5
6
7
8
9
10
11
12
zero : 0
one : 1
half : 0.5
quarter : 0.25
infinity : ∞
1 / 3
separatorSpaces can not be null
True
True
True
0/1
0/0

1. Auto-property initializers 自动属性初始化器

public int B { get ; set ; } = 1;

可以直接给自动属性赋值了,不需要写在构造函数中了。

2. Getter-only auto-properties 只读自动属性

public string SeparatorSpaces { get; } = string.Empty;

只读自动属性可以直接初始化,或者在构造函数中初始化。

3. Expression-bodied members 表达式体成员

public double Value => (double)A / B;

public int this[int index] => index == 0 ? A : B;

public void Print() => WriteLine(ToString());

只读属性,只读索引器和方法都可以使用Lambda表达式作为Body。

4. String interpolation 字符串嵌入值

"{A}{SeparatorSpaces}{Separator}{SeparatorSpaces}{B}";

反斜杠后的大括号中的表达式会在运行时计算值,并嵌入到字符串中。和Swift中很像,Swift中是反斜杠加小括号,C#是反斜杠加大括号。

5. nameof operator  nameof 运算符

throw new ArgumentNullException(nameof(separatorSpaces));

nameof会返回变量,参数或成员名。

这个很有用,原来写WPF中的ViewModel层的属性变化通知时,需要写字符串,或者使用MvvmLight等库中的帮助方法,可以直接传入属性,但由于是在运行时解析,会有少许性能损失。现在好了,使用nameof运算符,既能保证重构安全和可读性,又能保证性能。

6. Dictionary initializer 字典初始化器

new Dictionary< string, Fraction>

{

    ["zero"] = new Fraction (),

    ["one"] = new Fraction (1, 1),

    ["half"] = new Fraction (1, 2),

    ["quarter"] = new Fraction (1, 4),

    [ "infinity"] = new Fraction (1, 0),

};

现在字典可以用一种可读性更好的方法进行初始化,方括号包围的Key等于Value。

7. Parameterless struct ctors 无参数的结构体构造函数

public FractionStruct( int a, int b) { A = a; B = b; }

public FractionStruct() : this(0, 1) { }

结构体可以提供自定义的无参数构造函数了。

new FractionStruct()

default(FractionStruct)

new是调用无参数构造函数。

default是不调用无参数构造函数。

8. Exception filters 异常过滤器

catch (ArgumentNullException e) if (e.ParamName == "separatorSpaces")

{

    WriteLine("separatorSpaces can not be null");

}

设置进入catch块的条件。

9. Null propagation 空传播

v?.A

v?["B"]

v?.ToString()

对象为null时不调用属性,索引器,方法等,表达式返回null,和Swift中的用法相似。

10. Using static members using静态导入

using System.Console ;

WriteLine("separatorSpaces can not be null" );

在using中可以指定一个静态类,然后可以在随后的代码中直接使用该类的静态方法,和Java中的用法相似。

11. Await in catch/finally catch和finally块中的await

例子如下, 
Resource res = null; 
try 

    res = await Resource.OpenAsync(…); // You could do this. 
    … 

catch (ResourceException e) 

    await Resource.LogAsync(res, e); // Now you can do this … 

finally 

    if (res != null) await res.CloseAsync(); // … and this. 
}

 
分类: C#
原文地址:https://www.cnblogs.com/Leo_wl/p/4217507.html