[译文] C# 8 已成旧闻, 向前, 抵达 C# 9!

C# 8 is old news. Onward, to C# 9! (C# 8 已成旧闻, 向前, 抵达 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.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/312484.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

考察对顺序表的理解

顺序表是在计算机内存中以数组的形式保存的线性表 线性表的顺序存储是指用一组地址连续的存储单元依次存储线性表中的各个元素、使得线性表中在逻辑结构上相邻的数据元素存储在相邻的物理存储单元中,即通过数据元素物理存储的相邻关系来反映数据元素之间逻辑上的相邻…

2020年你应该学习 .Net Core

一、什么是.NET Core.NET Core是一个开源通用的开发框架,支持跨平台,即支持在Window,macOS,Linux等系统上的开发和部署,并且可以在硬件设备,云服务,和嵌入式/物联网方案中进行使用。.NET Core的…

对表头指针、表头结点,单链表删除的理解

https://blog.csdn.net/weixin_46678290/article/details/105309156

C# WPF发票打印

C# WPF发票打印内容目录实现效果业务场景编码实现本文参考源码下载1.实现效果发票界面PDF打印结果2.业务场景界面作为发票预览,按客户需求可打印成发票纸张给客户。3.编码实现3.1 添加Nuget库站长使用 .Net Core 3.1 创建的WPF工程,创建“Invoice”解决方…

对全局变量,static静态变量的理解

如果所有的变量按照下面的程序进行定义和声明,那么在main()函数中所有可用的变量为 ()。 void fun(int x) {static int y;……return; } int z; void main( ) {int a,b;fun(a);…… }A.x,y B.x,y,z C.a,b,z D.a,b,x,y,z static静态变量的值在…

dotNET知音,19年归档

2019年下半年开通公众号,尝试着分享和技术交流,也很高兴认识很多NETer同行。为了方便阅读,进行归档,如果之前有错过的文章,这是一个很好的补课机会。.NETCore3.0:《.Net Core3.0 配置Configuration》《.Net…

顺序表和链表的优缺点理解

若某线性表最常用的操作是存取任一指定序号的元素和在最后进行插入和删除运算,则利用哪种存储方式最节省时间?D A.双链表 B.单循环链表 C.带头结点的双循环链表 D.顺序表 想要存取任一指定序号的元素,链表实现这个功能的代价很大 本来顺序表的…

阿里如何应对亿级高并发大流量?如何保障高可用和稳定性!

作者:丁浪,目前在创业公司担任高级技术架构师。曾就职于阿里巴巴大文娱和蚂蚁金服。具有丰富的稳定性保障,全链路性能优化的经验。架构师社区特邀嘉宾!阅读本文,你将会收获: 高并发、大流量场景的常见问题和…

中缀转后缀

从中序表达式 转换为 后序表达式 例题: 利用栈对表达式19/(8-5)*4求值的过程中,操作数栈的最大容量是多少?(B )。 A.3 B.4 C.5 D.2 操作数栈:先是/(- , 然后遇到),则为/ &#xff0…

动手造轮子:写一个日志框架

动手造轮子:写一个日志框架Intro日志框架有很多,比如 log4net / nlog / serilog / microsoft.extensions.logging 等,如何在切换日志框架的时候做到不用修改代码,只需要切换不同的 loggingProvider 就可以了,最低成本的…

对循环队列的理解

循环队列存储在数组A[0…n-1]中,其头尾指针分别为f和r,头指针f总是指向队头元素,尾指针r总是指向队尾元素的下一个位置,则元素e入队时的操作为(B )。 A.A[r]e; rr1 B.A[r]e; r(r1)%n C.A[r]e;r(r1)%(n1) D.…

【C】@程序员,我们送给你一个成熟的Excel导入导出组件

程序员的显著特点有一天跟一位同事跟我闲聊,讨论起过去若干年软件行业的感受,他问了个问题:你觉得一个好的软件工程师最显著的特点是什么?我想了一会,说:大概是坐得住吧。某种意义上来说,在互联…

SummerBoot,将SpringBoot的先进理念与C#的简洁优雅合二为一

哈哈哈哈,大家好,我就是高产似母猪的三合,好久没写博客了,因为最近几个月在不断的加班,加班时长平均每个月120小时以上。今天是2020年的第一天,作为一条程序汪,觉得不做点啥好像对不起这个特别有…

树的度,结点,叶子结点,二叉树

设树T的度为4,其中度为1、2、3、4的结点个数分别为4、2、1、1。则T中有多少个叶子结点? A.4 B.6 C.8 D.10 一棵含有n个结点的树,有n-1个分支,即 n 14 22 31 41 1 16; 又由于 n n0 n1 n2 n3 n4 n0 8; n0 8 16&#…

C#刷遍Leetcode面试题系列连载(6):No.372 - 超级次方

点击蓝字“dotNET匠人”关注我哟加个“星标★”,每日 7:15,好文必达!前文传送门:C# 刷遍 Leetcode 面试题系列连载(1) - 入门与工具简介C#刷遍Leetcode面试题系列连载(2): No.38 - 报数C#刷遍Le…

堆 最大堆 最小堆

堆是特殊的队列 从堆中取出元素是依照元素的优先级大小,而不是元素进入队列的先后顺序 堆最常用的结构是二叉树表示,不特指的话,它是一棵完全二叉树 因为高度为h的完全二叉树有结点2(h-1) 到2h-1个,且结点排布及其规律&#xff…

使用 Postman 测试你的 API

使用 Postman 测试你的 APIIntro最近想对 API 做一些自动化测试,看了几个工具,最后选择了 postman,感觉 postman 的设计更好一些,我们可以在请求发送之前和请求获取到响应之后都可以自定义脚本,很灵活。而且 postman 的…

散列查找 散列表(哈希表)

哈希表的平均查找长度是()的函数。 A.哈希表的长度 B.哈希表的装填因子 C.哈希函数 D.表中元素的多少 装填因子 关键字个数 / 表长 符号表:是 “名字(Name)–属性(Attribute)”对的集合 符号表…

使用 postman 给 API 写测试

使用 postman 给 API 写测试Intro上次我们简单介绍了 使用 postman 测试 API,这次主要来写一些测试用例以检查请求的响应是否符合我们的预期以及如何使用脚本测试使用 postman 内置的随机变量postman 内置的有一些产生随机值的变量,在发送请求时随机生成…