[譯文] C# 8 已成舊聞, 向前, 抵達 C# 9!


原文鏈接: C# 8 is old news. Onward, to C# 9!

Did you know that planning is already underway for the ninth version of the C# language?

第九版 C# 語言已經在開發中了, 你曉得伐?

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.

現在, 公平地講, 在 C# 8 於2019年9月交付給開發者之前, 這個已經在計划階段 很久很久很久 了. 可以從一些關於 C# 9 的 issues 的討論中看到. 大多數人不太會遵循語言本身的日常規划(連我自己都是), 但時不時地參與下討論還是蠻有意思.

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!)

並且, 由於今天是聖誕節(作者是2019年12月25日寫的這篇文章), 所以讓我們悄悄咪咪地看下五個 (還有更多!) 關於C# 9 的 "禮物", 我們很有可能在2020年的某天收到(當然, 可能也不會!).

1. Simplified Parameter NULL Validation Code (簡化 NULL 參數驗證代碼)

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:

簡化版是通過在 方法參數 上帶有一個小注解以裝飾 參數, 這樣就不需要做 null 檢測或守衛子句, 從而簡化了內部的邏輯, 減少樣板驗證代碼. 比如:

// Before (之前)
void Insert(string s) {
 if (s is null) {
   throw new ArgumentNullException(nameof(s));
 }
 ...
}

// After (現在, 參數后面加了個嘆號)
void Insert(string s!) {
  ...
}

2. Switch expression as a statement expression (Switch 表達式作為語句表達式)

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 表達式作為 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 (主構造函數)

This one means to simplify all those boilerplate constructors, fields, property getters/setters, etc., that we’re so used to. For example:

意味着簡化所有的我們之前習慣的那些樣板構造函數, 字段, 屬性存取器等. 例如:

// 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:

與上面提到的基本結構在本質上稍微相似, 該建議的目的是在創造新類/結構體時, 消除編寫大量必要的樣板代碼. 如果 記錄 類型出現, 那么 主構造函數 就不再有了(意見). 例如:

//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 (可辨識聯合 / 枚舉類)

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:

這個讓我有點摸不着頭腦. 它使用我們都慣用的關鍵字(加上一個新關鍵字), 將它們組合在一起, 並將記錄 (上面提及的) 添加到混合中. 用我們自己話說, 它是創建抽象基類和繼承自它們的具體類型的 "更簡潔" 的方法. 比如:

(譯者注: Discriminated Unions, 可辨識聯合/可區分的聯合. 也稱變體類型(variant type), 通常是某一個枚舉類型, 包含一個聯合和一個標簽的數據結構. 能夠存儲一組不同但是固定的類型中某個類型的對象, 具體是哪個類型由標簽字段決定. 這種數據結構在解釋器, 數據庫和數據通信中非常有用. 鏈接: 標簽聯合)

// 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 正在進行的討論做的簡要介紹, 可以到 GitHub 項目看一看, 甚至可以參與進來! 這是一個新的全新的微軟, 我們的意見將受到歡迎!

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.

這個社區, 我們的社區, 是由非常棒棒的人組成的, 我超開心自己被認為是這個社區的中的一份子.

Whatever holiday you celebrate this time of year, I hope it’s wonderful.

不管每年的這個聖誕節慶祝什么, 我都希望它是棒棒噠.

See you next year, friends.

朋友們, 明年見!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM