C# 7.0 添加和增強的功能【基礎篇】

C# 7.0 版是與 Visual Studio 2017 一起發布 。雖然該版本繼承和發展了C# 6.0,但不包含編譯器即服務 。
一、out 變量以前我們使用out變量必須在使用前進行聲明,C# 7.0給我們提供了一種更簡潔的語法 “使用時進行內聯聲明”。如下所示:
var input = ReadLine();if (int.TryParse(input, out var num))//可直接寫明變量的類型也可以寫隱式類型,因為out關鍵字修飾的一定是局部變量{WriteLine("您輸入的數字是:{0}",num);}所謂的 “內聯聲明” 編譯后就是以前的原始寫法 , 只是現在由編譯器來完成 。即編譯后為:
int num;//提前聲明string s = Console.ReadLine();if (int.TryParse(s, out num)){Console.WriteLine("您輸入的數字是:{0}", num);}二、值元組(ValueTuple)使用元組語法,可以在一個語句中分配多個不同類型的變量 。以下是幾種常用的寫法:
//聲明命名項元組并為其分配元組值 , 再按名稱訪問元組項(string Name, string Capital, double GdpPerCapita) countryInfo = ("Malawi", "Lilongwe", 226.50);System.Console.WriteLine($@"The poorest country in the world in 2017 was {countryInfo.Name}, {countryInfo.Capital}: {countryInfo.GdpPerCapita}");//將元組分配到各個已聲明且使用分布式語法進行隱式類型化的變量中var (country, capital, gdpPerCapita) = ("Malawi", "Lilongwe", 226.50);System.Console.WriteLine($@"The poorest country in the world in 2017 was {country}, {capital}: {gdpPerCapita}");//將命名項元組分配到一個隱式類型化變量中 , 再按名稱訪問元組項var countryInfo = (Name:"Malawi", Capital:"Lilongwe", GdpPerCapita:226.50);System.Console.WriteLine($@"The poorest country in the world in 2017 was {countryInfo.Name}, {countryInfo.Capital}: {countryInfo.GdpPerCapita}");//將未命名的元組分配到一個隱式類型化變量中,再按 Item-number 屬性訪問元組元素var countryInfo = ("Malawi", "Lilongwe", 226.50);System.Console.WriteLine($@"The poorest country in the world in 2017 was {countryInfo.Item1}, {countryInfo.Item2}: {countryInfo.Item3}");//使用下劃線放棄相應的元組部分(string name, _, double gdpPerCapita) countryInfo = ("Malawi", "Lilongwe", 226.50);元組是用于將數據封裝到一個對象的輕型解決方案,就像用袋子將從商店購買的雜項物品裝在一起一樣 。
與數組不同,元組包含的項數據類型可以不同,幾乎不受任何限制(盡管不允許使用指針) , 但需要由代碼標識,且不能在運行時更改 , 這兩點限制除外 。
此外 , 不同于數組的是,編譯時也會對元組內的項數量進行硬編碼 。
另外,元組(Tuple)在 .Net 4.0 的時候就有了,值元組(ValueTuple)的出現,彌補了老版的兩個缺點:
??1)ValueTuple 支持語義上的字段命名 。舍棄僅按照順序取值(Item1,Item2...),增強了可讀性 。
??2)ValueTuple 是值類型(Struct) 。舍棄原來的引用類型,更為輕量化 。
三、析構元組和其他類型在 C# 7.0 及更高版本中,用戶可從元組中檢索多個元素,或者在單個析構操作中從對象檢索多個字段值、屬性值和計算值 。若要析構元組,可將其元素分配給各個變量 。析構對象時 , 將選定值分配給各個變量 。
原來的做法是:一旦檢索到元組 , 就必須處理它的各個元素 。按元素逐個操作比較麻煩,如下例所示 。 QueryCityData 方法返回一個三元組,并通過單獨的操作將其每個元素分配給一個變量 。
public class Example{public static void Main(){var result = QueryCityData("New York City"); // 返回全部字段var city = result.Item1; // 再逐個賦值var pop = result.Item2;var size = result.Item3;}private static (string, int, double) QueryCityData(string name){if (name == "New York City")return (name, 8175133, 468.48);return ("", 0, 0);}}用戶作為類、結構或接口的創建者,可通過實現一個或多個 Deconstruct方法來析構該類型的實例 。該方法返回 void , 且要析構的每個值由方法簽名中的 out 參數指示 。
新寫法示例:重載 Deconstruct 方法以返回 Person 對象的各種屬性組合 。
using System;public class Person{public string FirstName { get; set; }public string MiddleName { get; set; }public string LastName { get; set; }public string City { get; set; }public string State { get; set; }//構造函數 , 初始化賦值public Person(string fname, string mname, string lname, string cityName, string stateName){FirstName = fname;MiddleName = mname;LastName = lname;City = cityName;State = stateName;}// 返回名字和姓氏public void Deconstruct(out string fname, out string lname){fname = FirstName;lname = LastName;}// 返回名字、中間名和姓氏public void Deconstruct(out string fname, out string mname, out string lname){fname = FirstName;mname = MiddleName;lname = LastName;}// 返回名字、姓氏、城市、州public void Deconstruct(out string fname, out string lname, out string city, out string state){fname = FirstName;lname = LastName;city = City;state = State;}}public class ExampleClassDeconstruction{public static void Main(){var p = new Person("John", "Quincy", "Adams", "Boston", "MA");//初始化// Deconstruct the person object.var (fName, lName, city, state) = p;//自動調用對應的析構方法Console.WriteLine($"Hello {fName} {lName} of {city}, {state}!");}}// The example displays the following output://Hello John Adams of Boston, MA!

推薦閱讀