[译文] C# 8 已成旧闻, 向前, 抵达 C# 9!
<p>原文链接: C# 8 is old news. Onward, to C# 9!</p><blockquote>
<p>Did you know that planning is already underway for the ninth version of the C# language?</p>
</blockquote>
<p>第九版 C# 语言已经在开发中了, 你晓得伐?</p>
<blockquote>
<p>Now, to be fair, this has been in the planning phases long, <strong>LONG</strong>, 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.</p>
</blockquote>
<p>现在, 公平地讲, 在 C# 8 于2019年9月交付给开发者之前, 这个已经在计划阶段 <strong>很久很久很久</strong> 了. 可以从一些关于 C# 9 的 issues 的讨论中看到. 大多数人不太会遵循语言本身的日常规划(连我自己都是), 但时不时地参与下讨论还是蛮有意思.</p>
<blockquote>
<p>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!)</p>
</blockquote>
<p>并且, 由于今天是圣诞节(作者是2019年12月25日写的这篇文章), 所以让我们悄悄咪咪地看下五个 (还有<strong>更多</strong>!) 关于C# 9 的 <strong>"礼物"</strong>, 我们很有可能在2020年的某天收到(当然, 可能也不会!).</p>
<h2 id="1-simplified-parameter-null-validation-code-简化-null-参数验证代码">1. Simplified Parameter NULL Validation Code (简化 NULL 参数验证代码)</h2>
<blockquote>
<p>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:</p>
</blockquote>
<p>简化版是通过在 <code>方法参数</code> 上带有一个小注解以装饰 <code>参数</code>, 这样就不需要做 null 检测或守卫子句, 从而简化了内部的逻辑, 减少样板验证代码. 比如:</p>
<pre><code class="language-csharp">// Before (之前)
void Insert(string s) {
if (s is null) {
throw new ArgumentNullException(nameof(s));
}
...
}
// After (现在, 参数后面加了个叹号)
void Insert(string s!) {
...
}
</code></pre>
<h2 id="2-switch-expression-as-a-statement-expression-switch-表达式作为语句表达式">2. Switch expression as a statement expression (Switch 表达式作为语句表达式)</h2>
<blockquote>
<p>This one is still in the discussion phase, but the general idea is to allow a switch expression as an expression statement. For example:</p>
</blockquote>
<p>这个仍处于讨论阶段, 但总体思路是允许将 switch 表达式作为 expression 语句. 例如:</p>
<pre><code class="language-csharp">private void M(bool c, ref int x, ref string s)
{
c switch { true => x = 1, false => s = null };
}
</code></pre>
<blockquote>
<p>or</p>
</blockquote>
<p>或者</p>
<pre><code class="language-csharp">private void M(bool c, ref int x, ref string s)
=> c switch { true => x = 1, false => s = null };
</code></pre>
<h2 id="3-primary-constructors-主构造函数">3. Primary Constructors (主构造函数)</h2>
<blockquote>
<p>This one means to simplify all those boilerplate constructors, fields, property getters/setters, etc., that we’re so used to. For example:</p>
</blockquote>
<p>意味着简化所有的我们之前习惯的那些样板构造函数, 字段, 属性存取器等. 例如:</p>
<pre><code class="language-csharp">// 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;
}
}
}
</code></pre>
<h2 id="4-record-记录">4. Record (记录)</h2>
<blockquote>
<p>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:</p>
</blockquote>
<p>与上面提到的基本结构在本质上稍微相似, 该建议的目的是在创造新类/结构体时, 消除编写大量必要的样板代码. 如果 <code>记录</code> 类型出现, 那么 <code>主构造函数</code> 就不再有了(意见). 例如:</p>
<pre><code class="language-csharp">//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);
</code></pre>
<h2 id="5-discriminated-unions--enum-class-可辨识联合--枚举类">5. Discriminated Unions / enum class (可辨识联合 / 枚举类)</h2>
<blockquote>
<p>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:</p>
</blockquote>
<p>这个让我有点摸不着头脑. 它使用我们都惯用的关键字(加上一个新关键字), 将它们组合在一起, 并将记录 (上面提及的) 添加到混合中. 用我们自己话说, 它是创建抽象基类和继承自它们的具体类型的 "更简洁" 的方法. 比如:</p>
<p>(译者注: Discriminated Unions, 可辨识联合/可区分的联合. 也称变体类型(variant type), 通常是某一个枚举类型, 包含一个联合和一个标签的数据结构. 能够存储一组不同但是固定的类型中某个类型的对象, 具体是哪个类型由标签字段决定. 这种数据结构在解释器, 数据库和数据通信中非常有用. 链接: 标签联合)</p>
<pre><code class="language-csharp">// 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);
}
</code></pre>
<blockquote>
<p>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!</p>
</blockquote>
<p>这五个提案仅仅只是围绕 C# 9 正在进行的讨论做的简要介绍, 可以到 GitHub 项目看一看, 甚至可以参与进来! 这是一个新的全新的微软, 我们的意见将受到欢迎!</p>
<blockquote>
<p>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.</p>
</blockquote>
<p>由于这是我今年最后一篇博文, 因此我要向在这项令人赞叹的活动中自干五的人致以深深的感谢.</p>
<blockquote>
<p>This community…OUR community, is comprised of amazing folks, and I consider myself grateful to even be considered a part of it.</p>
</blockquote>
<p>这个社区, 我们的社区, 是由非常棒棒的人组成的, 我超开心自己被认为是这个社区的中的一份子.</p>
<blockquote>
<p>Whatever holiday you celebrate this time of year, I hope it’s wonderful.</p>
</blockquote>
<p>不管每年的这个圣诞节庆祝什么, 我都希望它是棒棒哒.</p>
<blockquote>
<p>See you next year, friends.</p>
</blockquote>
<p>朋友们, 明年见!</p><br><br>
来源:https://www.cnblogs.com/xixixiao/p/csharp-8-is-old-news-onward-to-csharp-9.html
頁:
[1]