C# 6 新特性簡單總結(jié)

1.靜態(tài)的using聲明

? ?靜態(tài)的using聲明允許調(diào)用靜態(tài)方法時不使用類名:

// C# 5

using System;

Console.WriteLine("C# 5");

// C# 6

using static System.Console;

WriteLine("C# 6");

2.表達式體方法

? 表達式體方法只包含一個可以用Lambda語法編寫的語句:

// C# 5

public bool IsSquare(Rectangle rect)

{

????return rect.Width == rect.Height;

}

// C# 6

public bool IsSquare(Rectangle rect) => rect.Width == rect.Height;

3.表達式體屬性

? 與表達式體方法類似,只有g(shù)et存儲器的單行屬性可以用Lambda語法編寫:

// C# 5

public string FullName

{

????get

????{

????????return FirstName + " " + LastName;

? ? ?}

}

// C# 6

public string FullName => FirstName + " " + LastName;

4.自動實現(xiàn)的屬性初始化器

? 自動實現(xiàn)的屬性可以用屬性初始化器來初始化:

// C# 5

public class Person

{

? ? public Person()

? ? {

? ? ? ? Age = 24;

? ? }

? ? public int Age { get; set; }

}

// C# 6

public class Person

{

? ? public int Age { get; set; } = 24;

}

5.只讀的自動屬性

// C# 5

private readonly int _bookId;

public BookId

{

? ? get

? ? {

? ? ? ? return _bookId;

? ? }

}

// C# 6

private readonly int _bookId;

public BookId { get; }

6.nameof運算符

? 使用新的nameof運算符,可以訪問字段名、屬性名、方法名和類型名。這樣,在重構(gòu)時就不會遺漏名稱的改變:

// C# 5

public void Method(object o)

{

? ? if(o == null)

? ? {

? ? ? ? throw new ArgumentNullException("o");

? ? }

}

// C# 6

public void Method(object o)

{

? ? if(o == null)

? ? {

? ? ? ? throw new ArgumentNullException(nameof(o));

? ? }

}

7.空值傳播運算符

? 空值傳播運算符簡化了空值的檢查:

// C# 5

int? age = p ==null?null : p.Age;

// C# 6

int? age = p?.Age;

8.字符串插值

? 字符串插值刪除了對string.Format的調(diào)用,它不在字符串中使用編號的格式占位符,占位符可以包含表達式:

// C# 5

public override string ToString()

{

? ? return string.Format("{0},{1}", Title, Publisher);

}

// C# 6

public override string ToString() => $"{Title}{Publisher}";

9.字典初始化

// C# 5

var dic = new Dictionary<int, string>();

dic.Add(3, "three");

dic.Add(7, "seven");

// C# 6

var dic = new Dictionary<int, string>()

{

? ? [3] = "three",

? ? [7] = "seven"

};

10.異常過濾器

? 異常過濾器允許在捕獲異常之前過濾它們:

// C# 5

try

{

? ? ? //

}

catch (MyException ex)

{

? ? if (ex.ErrorCode != 405) throw;

}

// C# 6

try

{

? ? ? //

}

catch (MyException ex) when (ex.ErrorCode == 405)

{

? ? //

}

11. 在catch和finally塊中使用await

? 在C#5中引入一對關(guān)鍵字await/async,用來支持新的異步編程模型,使的C#的異步編程模型進一步的簡化。在C#5中雖然引入了await/async,但是卻有一些限制,比如不能再catch和finally語句塊中使用,C#6中將不再受此限制。

private static void Main(string[] args)

? ? ? ? {

? ? ? ? ? ? do

? ? ? ? ? ? {

? ? ? ? ? ? ? ? Log(ConsoleColor.White, "caller method begin", true);

? ? ? ? ? ? ? ? CallerMethod();

? ? ? ? ? ? ? ? Log(ConsoleColor.White, "caller method end");

? ? ? ? ? ? } while (Console.ReadKey().Key != ConsoleKey.Q);

? ? ? ? }

? ? ? ? public static async void CallerMethod()

? ? ? ? {

? ? ? ? ? ? try

? ? ? ? ? ? {

? ? ? ? ? ? ? ? Log(ConsoleColor.Yellow, "try ", true);

? ? ? ? ? ? ? ? throw new Exception();

? ? ? ? ? ? }

? ? ? ? ? ? catch (Exception)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? Log(ConsoleColor.Red, "catch await begin", true);

? ? ? ? ? ? ? ? await AsyncMethod();

? ? ? ? ? ? ? ? Log(ConsoleColor.Red, "catch await end");

? ? ? ? ? ? }

? ? ? ? ? ? finally

? ? ? ? ? ? {

? ? ? ? ? ? ? ? Log(ConsoleColor.Blue, "finally await begin", true);

? ? ? ? ? ? ? ? await AsyncMethod();

? ? ? ? ? ? ? ? Log(ConsoleColor.Blue, "finally await end");

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? private static Task AsyncMethod()

? ? ? ? {

? ? ? ? ? ? return Task.Factory.StartNew(() =>

? ? ? ? ? ? {

? ? ? ? ? ? ? ? Log(ConsoleColor.Green, "async method begin");

? ? ? ? ? ? ? ? Thread.Sleep(1000);

? ? ? ? ? ? ? ? Log(ConsoleColor.Green, "async method end");

? ? ? ? ? ? });

? ? ? ? }

? ? ? ? private static void Log(ConsoleColor color, string message, bool newLine = false)

? ? ? ? {

? ? ? ? ? ? if (newLine)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? Console.WriteLine();

? ? ? ? ? ? }

? ? ? ? ? ? Console.ForegroundColor = color;

? ? ? ? ? ? Console.WriteLine($"{message,-20} : {Thread.CurrentThread.ManagedThreadId}");

? ? ? ? }


結(jié)果

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容