[译文] C# 8 已成旧闻, 向前, 抵达 C# 9!
C# 8 is old news. Onward, to C# 9! (C# 8 已成舊聞, 向前, 抵達(dá) C# 9!)
Did you know that planning is already underway for the ninth version of the C# language?
第九版 C# 語言已經(jīng)在開發(fā)中了, 你曉得伐?
Now, to be fair, this has been in the planning phases long,?LONG, before C# 8 shipped to us back in September 2019, as you can see from some of the discussion on the issues themselves. Most folks don’t follow the day-to-day planning of the language itself (myself included), but it’s interesting to peer into the discussions every now and then.
現(xiàn)在, 公平地講, 在 C# 8 于2019年9月交付給開發(fā)者之前, 這個已經(jīng)在計劃階段?很久很久很久?了. 可以從一些關(guān)于 C# 9 的 issues 的討論中看到. 大多數(shù)人不太會遵循語言本身的日常規(guī)劃(連我自己都是), 但時不時地參與下討論還是蠻有意思.
And, since this is Christmas Day, let’s peek in on five (there are a LOT more!) C# 9 language “gifts” we might receive sometime in 2020 (subject to change, of course!)
并且, 由于今天是圣誕節(jié)(作者是2019年12月25日寫的這篇文章), 所以讓我們悄悄咪咪地看下五個 (還有更多!) 關(guān)于C# 9 的?"禮物", 我們很有可能在2020年的某天收到(當(dāng)然, 可能也不會!).
1.?Simplified Parameter NULL Validation Code (簡化 NULL 參數(shù)驗證代碼)
The short version is that by decorating the value of a parameter to a method with a small annotation, we simplify the internal logic by not needing null validation / guard clauses, thus reducing boilerplate validation code. For example:
簡化版是通過在?方法參數(shù)?上帶有一個小注解以裝飾?參數(shù), 這樣就不需要做 null 檢測或守衛(wèi)子句, 從而簡化了內(nèi)部的邏輯, 減少樣板驗證代碼. 比如:
// Before (之前) void Insert(string s) {if (s is null) {throw new ArgumentNullException(nameof(s));}... }// After (現(xiàn)在, 參數(shù)后面加了個嘆號) void Insert(string s!) {... }2.?Switch expression as a statement expression (Switch 表達(dá)式作為語句表達(dá)式)
This one is still in the discussion phase, but the general idea is to allow a switch expression as an expression statement. For example:
這個仍處于討論階段, 但總體思路是允許將 switch 表達(dá)式作為 expression 語句. 例如:
private void M(bool c, ref int x, ref string s) {c switch { true => x = 1, false => s = null }; }or
或者
private void M(bool c, ref int x, ref string s)=> c switch { true => x = 1, false => s = null };3.?Primary Constructors (主構(gòu)造函數(shù))
This one means to simplify all those boilerplate constructors, fields, property getters/setters, etc., that we’re so used to. For example:
意味著簡化所有的我們之前習(xí)慣的那些樣板構(gòu)造函數(shù), 字段, 屬性存取器等. 例如:
// From This: (從這種形式) class Person {private string _firstName;public Person(string firstName){_firstName = firstName;}public string FirstName{get => _firstName;set {if (value == null) {throw new NullArgumentException(nameof(FirstName));}_firstName = value;}} }//To This: (到這種形式) class Person(string firstName) {public string FirstName{get => firstName;set {if (value == null){throw new NullArgumentException(nameof(FirstName));}firstName = value;}} }4.?Record (記錄)
Slightly similar in nature to Primary Constructions (mentioned above), the goal of this proposal is to remove the necessity of writing so much boilerplate code when creating a new class / struct. It seems possible that if Record types make it in, that Primary Constructors will not (opinion). For example:
與上面提到的基本結(jié)構(gòu)在本質(zhì)上稍微相似, 該建議的目的是在創(chuàng)造新類/結(jié)構(gòu)體時, 消除編寫大量必要的樣板代碼. 如果?記錄?類型出現(xiàn), 那么?主構(gòu)造函數(shù)?就不再有了(意見). 例如:
//From Something Like This: (從類似于這樣的) public class Person {public string Name { get; }public DateTime DateOfBirth { get; }public Person(string Name, DateTime DateOfBirth){this.Name = Name;this.DateOfBirth = DateOfBirth;} }//To Something Like This (到類似于這樣) public class Person(string Name, DateTime DateOfBirth);5.?Discriminated Unions / enum class (可辨識聯(lián)合 / 枚舉類)
This one took a smidge to wrap my brain around. It uses keywords that we’re all used to (plus a new one), combines them together, and adds Record’s (mentioned above) into the mix. It is, in my own words, a “cleaner” way of creating abstract base classes, and concrete types that inherit from them. For example:
這個讓我有點摸不著頭腦. 它使用我們都慣用的關(guān)鍵字(加上一個新關(guān)鍵字), 將它們組合在一起, 并將記錄 (上面提及的) 添加到混合中. 用我們自己話說, 它是創(chuàng)建抽象基類和繼承自它們的具體類型的 "更簡潔" 的方法. 比如:
(譯者注: Discriminated Unions, 可辨識聯(lián)合/可區(qū)分的聯(lián)合. 也稱變體類型(variant type), 通常是某一個枚舉類型, 包含一個聯(lián)合和一個標(biāo)簽的數(shù)據(jù)結(jié)構(gòu). 能夠存儲一組不同但是固定的類型中某個類型的對象, 具體是哪個類型由標(biāo)簽字段決定. 這種數(shù)據(jù)結(jié)構(gòu)在解釋器, 數(shù)據(jù)庫和數(shù)據(jù)通信中非常有用. 鏈接:?標(biāo)簽聯(lián)合)
// From Something Like This: (從類似于這樣的) public partial abstract class Shape { }public class Rectangle : Shape {public float Width { get; }public float Length { get; }public Rectangle(float Width, float Length){this.Width = Width;this.Length = Length;} }public class Circle : Shape {public float Radius { get; }public Circle(float Radius){this.Radius = Radius;} }// To Something Like This: (到類似于這樣的) enum class Shape {Rectangle(float Width, float Length);Circle(float Radius); }These five proposals only skim the surface of the discussions going on around C# 9. Head over to the GitHub project, take a look, and even get involved! It’s a new Microsoft, and our opinions are welcome!
這五個提案僅僅只是圍繞 C# 9 正在進(jìn)行的討論做的簡要介紹, 可以到 GitHub 項目看一看, 甚至可以參與進(jìn)來! 這是一個新的全新的微軟, 我們的意見將受到歡迎!
Since I’m the final post of the year, I’d like to offer a major thank you to everyone that volunteered, wrote, tweeted, retweeted, liked, hearted, shared, read, etc., to this amazing event.
由于這是我今年最后一篇博文, 因此我要向在這項令人贊嘆的活動中自干五的人致以深深的感謝.
This community…OUR community, is comprised of amazing folks, and I consider myself grateful to even be considered a part of it.
這個社區(qū), 我們的社區(qū), 是由非常棒棒的人組成的, 我超開心自己被認(rèn)為是這個社區(qū)的中的一份子.
Whatever holiday you celebrate this time of year, I hope it’s wonderful.
總結(jié)
以上是生活随笔為你收集整理的[译文] C# 8 已成旧闻, 向前, 抵达 C# 9!的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 轻量级开源小程序SDK发车啦
- 下一篇: C# WPF发票打印