VS2015中C#版本6.0的新特性 你需要知道
本文列出個(gè)人感覺比較有用的幾個(gè)新功能,供大家參考,具體內(nèi)容如下
注意:這些新特性只能用于VS2015及更高版本,無法在VS2013、VS2010等低版本中使用。當(dāng)然,如果你不喜歡這些新的特性,仍然可以繼續(xù)使用原來的用法(所以說它是新的語法糖)。
1、自動(dòng)屬性初始化的改進(jìn)(有用)
原來的用法(聲明時(shí)無法同時(shí)初始化),例如:
class MyClass
{
public int Age { get; set; }
public string Name { get; set; }
public MyClass()
{
Age = 20;
Name = "張三";
}
}
新用法(聲明時(shí)可同時(shí)初始化,更方便了),例如:
class MyClass
{
public int Age { get; set; } = 20;
public string Name { get; set; } = "張三";
}
2、String.Format的改進(jìn)(有用)
原來的用法:用string.Format(…)實(shí)現(xiàn),例如:
class MyClass
{
public void MyMethod()
{
string name = "張三";
int age = 20;
string s1 = string.Format("{0},{1}", name, age);
string s2 = string.Format("姓名={0},年齡={1}", name, age);
string s3 = string.Format("{0,15},{1:d3}", name, age);
string s4 = string.Format("{0,15},{1,10:d3}", name, age);
Console.WriteLine("{0},{1},{2},{3}", s1, s2, s3 ,s4);
string s5 = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
}
}
新用法:用“$”前綴實(shí)現(xiàn)(變量直接寫到大括號(hào)內(nèi),而且?guī)е悄芴崾?,更方便了),例如?nbsp;
class MyClass
{
public void MyMethod()
{
string name = "張三";
int age = 20;
string s1 = $"{name},{age}";
string s2 = $"姓名={name},年齡={age}";
string s3 = $"{name,15},{age:d3}";
string s4 = $"{name,15},{age,10:d3}";
Console.WriteLine($"{s1},{s2},{s3},{s4}");
string s5 = $"{DateTime.Now:yyyy-MM-dd}";
}
}
3、字典的初始化
原來的用法:
class MyClass
{
public void MyMethod()
{
Dictionary<string, int> student = new Dictionary<string, int>();
student.Add("a1", 15);
student.Add("a2", 14);
student.Add("a3", 16);
}
}
新用法(可以直接寫初始化的值,更方便了):
class MyClass
{
public void MyMethod()
{
Dictionary<string, int> student = new Dictionary<string, int>()
{
["a1"] = 15,
["a2"] = 14,
["a3"] = 16
};
}
}
4、可以用static聲明靜態(tài)類的引用
原來的用法:
using System;
namespace MyApp
{
class Demo1New
{
public static double MyMethod(double x, double angle)
{
return Math.Sin(x) + Math.Cos(angle);
}
}
}
新用法(表達(dá)式比較復(fù)雜的時(shí)候有用,代碼更簡潔了):
using static System.Math;
namespace MyApp
{
class Demo1New
{
public static double MyMethod(double x, double angle)
{
return Sin(x) + Cos(angle);
}
}
}
5、nameof表達(dá)式
假定WPF應(yīng)用程序中有下面的類:
public class MyClass
{
public string MyText { get; set; } = "aaa";
}
并假定有下面的XAML代碼:
<StackPanel>
<TextBlock Name="txt1"/>
……
</StackPanel>
代碼隱藏類中原來的用法:
txt1.SetBinding(TextBlock.TextProperty, "MyText");
現(xiàn)在的用法(因?yàn)橛绣e(cuò)誤檢查智能提示,用起來更方便了):
txt1.SetBinding(TextBlock.TextProperty, nameof(MyClass.MyText));
6、Null-條件表達(dá)式
(有用)
var ss = new string[] { "Foo", null };
var length0 = ss [0]?.Length; // 結(jié)果為3
var length1 = ss [1]?.Length; // 結(jié)果為null
var lengths = ss.Select (s => s?.Length ?? 0); //結(jié)果為[3, 0]
7、在try-catch-finally中使用await
異步編程中,原來在catch或者finally中無法使用await,現(xiàn)在可以了:
async void SomeMethod()
{
try
{
//...etc...
}
catch (Exception x)
{
var diagnosticData = await GenerateDiagnosticsAsync (x);
Logger.log (diagnosticData);
}
finally
{
await someObject.FinalizeAsync();
}
}
8、其他
C# 6.0還有一些新的特性,對(duì)于初學(xué)者來說用的不是太多,所以這里就不再介紹了。
再次說明一下,如果你不喜歡新的特性,仍然可以繼續(xù)使用原來的用法。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
asp.net中的“按需打印”(打印你需要打印的部分) 實(shí)現(xiàn)代碼
有時(shí)我們需要對(duì)asp.net網(wǎng)頁中某些特定部分進(jìn)行打印,很多人采用CSS樣式或特定的打印控件來解決網(wǎng)頁定制打印功能。這里采用Javascript樣式替換方式進(jìn)行打印,使網(wǎng)頁顯示與打印效果相分離。2013-06-06
http轉(zhuǎn)https的實(shí)戰(zhàn)記錄(iis 7.5)
這篇文章主要給大家介紹了關(guān)于http轉(zhuǎn)https的相關(guān)資料,文中是最近的一次實(shí)戰(zhàn)記錄,基于iis7.5,通過一步步的圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2018-01-01
ASP.NET?Core?MVC自定義Tag?Helpers用法介紹
這篇文章介紹了ASP.NET?Core?MVC自定義Tag?Helpers的用法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02
asp.net下XML的加密和解密實(shí)現(xiàn)方法
xml加密(XML Encryption)是w3c加密xml的標(biāo)準(zhǔn)。這個(gè)加密過程包括加密xml文檔的元素及其子元素,通過加密,xml的初始內(nèi)容將被替換,但其xml格式仍然被完好的保留。2010-02-02
asp.net使用ashx生成圖形驗(yàn)證碼的方法示例
這篇文章主要介紹了asp.net使用ashx生成圖形驗(yàn)證碼的方法,結(jié)合實(shí)例形式分析了asp.net生成圖形驗(yàn)證碼的步驟、實(shí)現(xiàn)方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-07-07
Global.asax的Application_Error實(shí)現(xiàn)錯(cuò)誤記錄/錯(cuò)誤日志的代碼
本文為大家介紹下利用Global.asax的Application_Error實(shí)現(xiàn)錯(cuò)誤記錄,具體如下,有此需求的朋友可以參考下,希望對(duì)大家有所幫助2013-08-08
ASP.net 驗(yàn)證碼實(shí)現(xiàn)代碼(C#)
asp.net 驗(yàn)證碼效果實(shí)現(xiàn)代碼2008-02-02

