最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C# 6.0 新特性匯總

 更新時間:2016年09月28日 13:43:16   作者:我打農(nóng)村來  
這篇文章主要介紹了C# 6.0 新特性匯總的相關(guān)資料,本文給大家?guī)砹?1種新特征,非常不錯,感興趣的朋友一起看看吧

1. 靜態(tài)using(static using)

靜態(tài)using聲明允許不使用類名直接調(diào)用靜態(tài)方法。

The static using declaration allows invoking static methods without the class
name.
In C# 5
using System;
Console.WriteLine("Hello, World!");
In C# 6
using static System.Console;
WriteLine("Hello, World");

2. 表達式方法(Expression-Bodied Methods)

使用表達式方法,只有一條語句的方法可以使用lambda語法寫。

With expression-bodied methods, a method that includes just one statement can
be written with the lambda syntax.
In C# 5
public bool IsSquare(Rectangle rect)
{
return rect.Height == rect.Width;
}
In C# 6
public bool IsSquare(Rectangle rect) => rect.Height == rect.Width;

3. 表達式屬性(Expression-Bodied Properties)

跟表達式方法類似,只有一個get訪問器的單行屬性可以使用lambda語法寫。

Similar to expression-bodied methods, one-line properties with only a get accessor
can be written with the lambda syntax
In C# 5
public string FullName
{
get
{
return FirstName +"" + LastName;
}
}
In C# 6
public string FullName => FirstName +"" + LastName;

4. 自動屬性初始化器(Auto-Implemented Property Intializers)

自動屬性可以使用屬性初始化器初始化。

Auto-implemented properties can be initialized with a property initializer.

In C# 5
public class Person
{
public Person()
{
Age = 24;
}
public int Age {get; set;}
}
In C# 6
public class Person
{
public int Age {get; set;} = 42;
}

5. 只讀自動屬性(Read-Only Auto Properties)

C# 5需要完整的屬性語法實現(xiàn)只讀屬性,C# 6可以使用自動屬性實現(xiàn)。

To implement read-only properties, C# 5 requires the full property syntax. With
C# 6, you can do this using auto-implemented properties.
In C# 5
private readonly int _bookId;
public BookId
{
get
{
return _bookId;
}
}
In C# 6
public BookId {get;}

6. nameof操作符(nameof Operator)

字段、屬性、方法和類型的name可以通過nameof訪問。使用nameof,可以方便的重構(gòu)name變化。

With the new nameof operator, names of fields, properties, methods, or types can
be accessed. With this, name changes are not missed with refactoring.

In C# 5
public void Method(object o)
{
if (o == null) throw new ArgumentNullException("o");
In C# 6
public void Method(object o)
{
if (o == null) throw new ArgumentNullException(nameof(o));

7. Null傳遞操作符(Null Propagation Operator)

Null傳遞操作符簡化了空值檢查。

The null propagation operator simplifies null checks.
In C# 5
int? age = p == null ? null : p.Age;
var handler = Event;
if (handler != null)
{
handler(source, e);
}
In C# 6
int? age = p?.Age;
handler?.Invoke(source, e);

8. 字符串插值(String Interpolation)

字符串差值移除了對string.Format的調(diào)用,使用表達式占位符取代數(shù)字格式占位符。

The string interpolation removes calls to string.Format. Instead of using
numbered format placeholders in the string, the placeholders can include
expressions.
In C# 5
public override ToString()
{
return string.Format("{0}, {1}", Title, Publisher);
}
In C# 6
public override ToString() => $"{Title} {Publisher}";

9. 字典初始化器(Dictionary Initializers)

字典可以使用類似集合的字典初始化器初始化。

Dictionaries can now be initialized with a dictionary initializer—similar to the
collection initializer.
In C# 5
var dict = new Dictionary<int, string>();
dict.Add(3,"three");
dict.Add(7,"seven");
In C# 6
var dict = new Dictionary<int, string>()
{
[3] ="three",
[7] ="seven"
};

10. 異常過濾器(Exception Filters)

異常過濾器允許你在捕獲異常前進行過濾。

Exception filters allow you to filter exceptions before catching them.

In C# 5
try
{
//etc.
} catch (MyException ex)
{
if (ex.ErrorCode != 405) throw;
// etc.
}
In C# 6
try
{
//etc.
} catch (MyException ex) when (ex.ErrorCode == 405)
{
// etc.
}

11. 在Catch使用Await(Await in Catch)

await可以在catch塊中直接使用,C# 5中需要變通使用。

await can now be used in the catch clause. C# 5 required a workaround.
In C# 5
bool hasError = false;
string errorMessage = null;
try
{
//etc.
} catch (MyException ex)
{
hasError = true;
errorMessage = ex.Message;
} 
if (hasError)
{
await new MessageDialog().ShowAsync(errorMessage);
}
In C# 6
try
{
//etc.
} catch (MyException ex)
{
await new MessageDialog().ShowAsync(ex.Message);
}

以上所述是小編給大家介紹的C# 6.0 新特性匯總,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • C#循環(huán)與循環(huán)控制的表達式樹實現(xiàn)

    C#循環(huán)與循環(huán)控制的表達式樹實現(xiàn)

    這篇文章介紹了C#循環(huán)與循環(huán)控制的表達式樹實現(xiàn),文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-01-01
  • C# 格式化JSON的兩種實現(xiàn)方式

    C# 格式化JSON的兩種實現(xiàn)方式

    本文主要介紹了C# 格式化JSON的兩種實現(xiàn)方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-05-05
  • C# double類型變量比較分析

    C# double類型變量比較分析

    這篇文章主要介紹了C# double類型變量比較分析,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • C#使用Data?Annotations進行手動數(shù)據(jù)驗證

    C#使用Data?Annotations進行手動數(shù)據(jù)驗證

    這篇文章介紹了C#使用Data?Annotations進行手動數(shù)據(jù)驗證的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • C#微信公眾號與訂閱號接口開發(fā)示例代碼

    C#微信公眾號與訂閱號接口開發(fā)示例代碼

    這篇文章主要介紹了C#微信公眾號與訂閱號接口開發(fā)示例代碼,結(jié)合實例形式簡單分析了C#針對微信接口的調(diào)用與處理技巧,需要的朋友可以參考下
    2016-06-06
  • c#實現(xiàn)在圖上畫漢字

    c#實現(xiàn)在圖上畫漢字

    這篇文章主要介紹了c#實現(xiàn)在圖上畫漢字方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • C#利用DesignSurface如何實現(xiàn)簡單的窗體設(shè)計器

    C#利用DesignSurface如何實現(xiàn)簡單的窗體設(shè)計器

    這篇文章主要介紹了C#利用DesignSurface如何實現(xiàn)簡單窗體設(shè)計器的相關(guān)資料,文中通過圖文及示例代碼介紹的很詳細,對大家具有一定的參考價值,需要的朋友們下面來一起學習學習吧。
    2017-02-02
  • C#無邊框窗體實現(xiàn)以及拖動代碼

    C#無邊框窗體實現(xiàn)以及拖動代碼

    我們給大家分享了關(guān)于C#無邊框窗體實現(xiàn)以及拖動代碼,大家在程序設(shè)計的時候如果用的到一起跟著小編學習下吧。
    2018-03-03
  • C#連接MySql數(shù)據(jù)庫的方法

    C#連接MySql數(shù)據(jù)庫的方法

    最近兩天在解決C#連接MySql數(shù)據(jù)庫的問題,通過不同的從網(wǎng)上學習,最終找到了解決的辦法,現(xiàn)在和大家分享一下
    2013-10-10
  • C#實現(xiàn)多個計時器記錄不同定時時間

    C#實現(xiàn)多個計時器記錄不同定時時間

    這篇文章主要為大家詳細介紹了C#實現(xiàn)多個計時器記錄不同定時時間,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-12-12

最新評論

漠河县| 临夏市| 武夷山市| 曲靖市| 昔阳县| 涪陵区| 新乡县| 屯昌县| 井研县| 安丘市| 胶州市| 广安市| 新密市| 平泉县| 丹寨县| 洛宁县| 寻乌县| 六枝特区| 滕州市| 特克斯县| 福贡县| 公主岭市| 吉木萨尔县| 成安县| 金门县| 二手房| 高唐县| 洱源县| 荥经县| 大足县| 贞丰县| 镇巴县| 宜良县| 桂平市| 太康县| 建宁县| 泽州县| 多伦县| 陇南市| 安塞县| 济宁市|