C#各个版本中的新增特性详解
序言
自從2000年初期發布以來,c#編程語言不斷的得到改進,使我們能夠更加清晰的編寫代碼,也更加容易維護我們的代碼,增強的功能已經從1.0搞到啦7.0甚至7.1,每一次改過都伴隨著.NET Framework庫的相應支持,也不斷的帶給我們期待與驚喜。下面我們就對c#一路走到現在,做一個回顧與學習。
C#語言目標與前行
c#的設計目標是有以下幾點:
旨在是一種簡單,現代,通用的面向對象編程語言。
語言及其實現應該為軟件工程原理提供支持,例如強類型檢查,數組維度檢查,未初始化的變量引用檢測以及自動垃圾收集。軟件的魯棒性,耐久性和程序員的生產力很重要。
旨在用于開發適用于分布式環境中部署的軟件組件。
便攜性對于源代碼和程序員非常重要,特別是已經熟悉C和C ++的程序員。
支持國際化是非常重要的。
C#適用于為托管和嵌入式系統編寫應用程序,從使用復雜的操作系統到非常小的專用功能都非常適用。
雖然C#應用程序在內存和處理能力要求方面是經濟的,但是該語言并不打算直接用C或匯編語言直接與性能和尺寸進行競爭。
c#版本
c#版本中添加的功能:
C#2.0?
泛型
部分類型
匿名方法
迭代器
可空類型
Getter / setter單獨可訪問性
方法組轉換(代表)
Co- and Contra-variance for delegates
靜態類
Delegate inference
C#3.0?
隱式類型局部變量
對象和收集初始化器
自動實現的屬性
匿名類型
擴展方法
查詢表達式
Lambda表達式
表達樹
部分方法
C#4.0?
動態綁定
命名和可選參數
Generic co- and contravariance
嵌入式互操作類型(“NoPIA”)
C#5.0?
異步方法
Caller info attributes
C#6.0?
Compiler-as-a-service(Roslyn)
將靜態類型成員導入命名空間
異常過濾器
在Catch和Finally中使用Await
自動屬性初始化器
只讀屬性的默認值
Expression-bodied members
Null-conditional operators(空條件運算符,簡潔檢查)
字符串插值
nameof operator
字典初始化器
C#7.0?
out變量
模式匹配
元組
解構
局部函數
數字分隔符
二進制文字
局部引用和引用返回
擴展異步返回類型
表達式的構造函數和finalizers
Expression bodied getters and setters
throw表達式
C#7.1?
Async main
默認表達式
參考文檔:https://en.wikipedia.org/wiki/C_Sharp_(programming_language)#cite_note-roslyn_6-47
c#7.0新特性詳解
在2016年Visual Studio “15” Preview 4發布,c# 7.0便開始浮現,現在vs2017已經發布,c#7.0帶給我們的驚喜,我們也該一探究竟啦。
C#7.0增加許多新功能,重點是數據,代碼簡化和性能上。
Out variables
目前在C#中,使用out參數不像我們想要的那么流暢。在使用out參數調用一個方法之前,首先必須聲明變量來傳遞給它。您也不能使用var它們來聲明它們,但需要指定完整的類型。
public void PrintCoordinates(Point p) {int x, y; //必須聲明p.GetCoordinates(out x, out y);WriteLine($"({x}, {y})"); }在c#7中再來看看
public void PrintCoordinates(Point p) {p.GetCoordinates(out int x, out int y);WriteLine($"({x}, {y})"); }模式匹配
c#7中
public static void PrintStars(object o) {if (o is int i)Console.WriteLine(i + 12); }
以前版本需要轉化
public static void PrintStars(object o) {if (o is int) Console.WriteLine(Convert.ToInt32(o) + 12); }Switch statements with patterns 擴展switch語句使用模式匹配
public static void PrintStars(object o)
? ? ? ? {
? ? ? ? ? ? switch (o)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? case Print p:
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case int a:
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case String b when b=="123":
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? } ? ? ? ?
? ? } ??
? ? public class Print
? ? {
? ? ? ? public string PrintName { get; set; }
? ? ? ? public string MoBanPath { get; set; }
? ? ? ? public int Count { get; set; }
? ? }
元組(Tuples)
注意:元組依賴于一組基礎類型,不包括在預覽4中。要使功能正常工作,您可以通過NuGet輕松獲取它們:
右鍵單擊解決方案資源管理器中的項目,然后選擇“管理NuGet軟件包...”
選擇“瀏覽”選項卡,選中“包含預發行”,然后選擇“nuget.org”作為“包源”
搜索“System.ValueTuple”并安裝它。
static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? var tuple = (a: 10, b: "123");
? ? ? ? ? ? Console.WriteLine($"a:{tuple.a},b:{tuple.b}");
? ? ? ? ? ? var result1 = GetS();
? ? ? ? ? ? var result = Get();
? ? ? ? ? ? Console.WriteLine($"Item1:{result1.Item1},Item2:{result1.Item2},Item3:{result1.Item3}");
? ? ? ? ? ? Console.WriteLine($"a:{result.a},b:{result.b},c:{result.c}");
? ? ? ? ? ? Console.ReadLine();
? ? ? ? }
? ? ? ? static (string, int, DateTime) GetS()
? ? ? ? {
? ? ? ? ? ? return ("abc", 123, DateTime.Now);
? ? ? ? }
? ? ? ? static (string a, int b, DateTime c) Get()
? ? ? ? {
? ? ? ? ? ? return (a: "abc", b: 123, c: DateTime.Now);
? ? ? ? }
部函數
簡單的說,就是在方法里面寫方法然后自己調用。
static void Main(string[] args){Console.WriteLine($"{Get(123)},{Get("abc")},{Get(null)}");Console.ReadLine();}public static string Get(object a){ return GetP();string GetP(){if (a is int v) return v + "";if (a is string b) return b;return "ccc";}}Literal improvements
C#7.0允許在數字文字中_作為數字分隔符出現:
var d = 123_456; var x = 0xAB_CD_EF;你可以把它們放在數字之間,以提高可讀性。它們對價值沒有影響。
另外,C#7.0引入了二進制文字,因此您可以直接指定位模式,而不必以心臟知道十六進制符號。?
var b = 0b1010_1011_1100_1101_1110_1111;更多c#7.0的特性
到此,我就不在一個個的上演c#7.0的其他功能啦。如果你想深入其他,仔細閱讀下面的文檔:
請參考文檔:https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/
c#6.0新語言功能詳解
以下新功能在VS 2015及17中實現并可用。
ADD:新增,Exists:已存在,N/A:對該語言沒有意義,No:不適用此版本。
下面我簡述幾個新功能,剩下的我會把文檔連接拿出來自己觀摩。
字符串插值
上面的代碼中都有體現。
var s = $"{p.Name} is {p.Age} year{{s}} old";字典索引初始化
var numbers = new Dictionary<int, string> {[7] = "seven",[9] = "nine",[13] = "thirteen" };自動屬性初始化
public class Customer {public string First { get; set; } = "Jane";public string Last { get; set; } = "Doe"; }using引用靜態類
using static System.Console; using static System.Math; using static System.DayOfWeek; class Program {static void Main(){WriteLine(Sqrt(3*3 + 4*4)); WriteLine(Friday - Monday); } }更多c#6.0語言新特性
線上代碼可執行演示文檔連接:http://www.volatileread.com/Wiki/Index?id=1075?效果如下
參考文檔:https://github.com/dotnet/roslyn/wiki/New-Language-Features-in-C%23-6
c#2.0-5.0參考文檔
c#5.0參考文檔
連接地址:https://blogs.msdn.microsoft.com/mvpawardprogram/2012/03/26/an-introduction-to-new-features-in-c-5-0/
c#4.0參考文檔
連接地址:https://msdn.microsoft.com/en-us/magazine/ff796223.aspx
c#3.0參考文檔
連接地址:https://msdn.microsoft.com/en-us/library/bb308966.aspx
c#2.0參考文檔
連接地址:https://msdn.microsoft.com/en-us/library/7cz8t42e(v=vs.80).aspx
原文地址:http://www.cnblogs.com/knowledgesea/p/6694979.html
.NET社區新聞,深度好文,微信中搜索dotNET跨平臺或掃描二維碼關注
總結
以上是生活随笔為你收集整理的C#各个版本中的新增特性详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在ASP.NET Core中使用Apwo
- 下一篇: ASP.NET Core 网站在Dock