最新国产好看的视频,伊人天堂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. 表達(dá)式方法(Expression-Bodied Methods)

使用表達(dá)式方法,只有一條語句的方法可以使用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. 表達(dá)式屬性(Expression-Bodied Properties)

跟表達(dá)式方法類似,只有一個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需要完整的屬性語法實(shí)現(xiàn)只讀屬性,C# 6可以使用自動屬性實(shí)現(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)用,使用表達(dá)式占位符取代數(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)

異常過濾器允許你在捕獲異常前進(jìn)行過濾。

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 新特性匯總,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

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

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

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

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

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

    C# double類型變量比較分析

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

最新評論

曲麻莱县| 荥经县| 临颍县| 宜兰市| 临清市| 栾川县| 郧西县| 烟台市| 股票| 乐山市| 金阳县| 江山市| 简阳市| 静海县| 彰武县| 诸暨市| 沂水县| 榆中县| 铜川市| 江川县| 高陵县| 巴南区| 浦县| 邯郸市| 楚雄市| 富锦市| 巴林右旗| 明光市| 通化市| 沙田区| 瓦房店市| 临海市| 崇文区| 岢岚县| 吴旗县| 福建省| 越西县| 栖霞市| 凤冈县| 天气| 德江县|