惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
A
About on SuperTechFans
IT之家
IT之家
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Blog — PlanetScale
Blog — PlanetScale
aimingoo的专栏
aimingoo的专栏
云风的 BLOG
云风的 BLOG
The GitHub Blog
The GitHub Blog
Vercel News
Vercel News
G
Google Developers Blog
J
Java Code Geeks
宝玉的分享
宝玉的分享
T
Tailwind CSS Blog
Cloudbric
Cloudbric
L
LINUX DO - 最新话题
MyScale Blog
MyScale Blog
H
Heimdal Security Blog
PCI Perspectives
PCI Perspectives
Attack and Defense Labs
Attack and Defense Labs
S
Security @ Cisco Blogs
Latest news
Latest news
I
Intezer
L
Lohrmann on Cybersecurity
C
CXSECURITY Database RSS Feed - CXSecurity.com
月光博客
月光博客
T
Threatpost
博客园 - 【当耐特】
S
Schneier on Security
P
Privacy International News Feed
G
GRAHAM CLULEY
T
Tenable Blog
AWS News Blog
AWS News Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
雷峰网
雷峰网
博客园 - Franky
Engineering at Meta
Engineering at Meta
美团技术团队
S
Secure Thoughts
T
Troy Hunt's Blog
Microsoft Security Blog
Microsoft Security Blog
SecWiki News
SecWiki News
V
Visual Studio Blog
人人都是产品经理
人人都是产品经理
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Cisco Talos Blog
Cisco Talos Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Martin Fowler
Martin Fowler
Webroot Blog
Webroot Blog
Google DeepMind News
Google DeepMind News
H
Hackread – Cybersecurity News, Data Breaches, AI and More

博客园 - Rwing

C# 13(.Net 9) 中的新特性 - 半自动属性 [翻译].NET 8 的原生AOT及高性能Web开发中的应用[附性能测试结果] C# 13(.Net 9) 中的新特性 - 扩展类型 如何在 Docker 环境下自动给 .NET 程序生成 Dump [翻译] 预览 C# 10 的新东西 [翻译]正式宣布 Visual Studio 2022 [翻译]Azure 网关迁移至 .NET Core 3.1 性能提升一倍 C# 9.0 新特性预览 - init-only 属性 C# 9.0 新特性预览 - 顶级语句 C# 9.0 新特性预览 - 空参数校验 [翻译] 使用 Serverless 和 .NET Core 构建飞速发展的架构 - Rwing [翻译] .NET Core 3.0 RC 1 发布 - Rwing [翻译] .NET Core 3.0 Preview 9 发布 - Rwing [翻译] .NET Core 3.0 Preview 7 发布 - Rwing [翻译] C# 8.0 接口默认实现 - Rwing [翻译] 正式宣布 .NET 5 - Rwing [翻译] ASP.NET Core 利用 Docker、ElasticSearch、Kibana 来记录日志 - Rwing [翻译] Visual Studio 2019: 极速编码. 智能工作. 创造未来. - Rwing [翻译] 使用 .NET Core 3.0 创建一个 Windows 服务 - Rwing
C# 9.0 新特性预览 - 类型推导的 new - Rwing
Rwing · 2020-05-06 · via 博客园 - Rwing

C# 9.0 新特性预览 - 类型推导的 new

前言

随着 .NET 5 发布日期的日益临近,其对应的 C# 新版本已确定为 C# 9.0,其中新增加的特性(或语法糖)也已基本锁定,本系列文章将向大家展示它们。

目录

[C# 9.0 新特性预览 - 类型推导的 new]
[C# 9.0 新特性预览 - 空参数校验]
[C# 9.0 新特性预览 - Lambda 中的弃元]
[C# 9.0 新特性预览 - Record 类型]
[C# 9.0 新特性预览 - 模式匹配的改善]
[C# 9.0 新特性预览 - 其他小的变化]

具有类型推导的 new 表达式 (Target-typed new expressions)

这是一个本应随着 C# 8.0 发布的语言特性,但因种种原因在发布 C# 8.0 的最后关头,它被移出了最终的发布版本,下面我们来认识认识它。
大家都知道,C# 在3.0中新增 var 关键字来做隐式类型声明,把繁重的声明语法简化了。

Dictionary<string, List<int>> field = new Dictionary<string, List<int>>()
// 可以简化为
var field = new Dictionary<string, List<int>>()

var 关键字的基本原理不再复述,简单说就是编译器可以根据等号后面的类型推导出 var 的类型,那么是不是也可以反过来,我们先声明类型,接下来的 new 关键字后面就不用写类型了呢?于是本文介绍的特性来了:

Dictionary<string, List<int>> field = new Dictionary<string, List<int>>()
// C# 9.0 中可以写成
Dictionary<string, List<int>> field = new()

从以上代码可以看出语法很简单,即省略了繁琐的可以推导出的类型。
其语法 Spec 如下:

'new' '(' argument_list? ')' object_or_collection_initializer?

搭配初始化器,我们可以进一步简化带有初始值的初始化。

Dictionary<string, List<int>> field = new() {
    { "item1", new() { 1, 2, 3 } }
};

进一步展示该语法在各种情况下的使用

在所有可以推导出类型的上下文中,都可以使用,例如:

XmlReader.Create(reader, new() { IgnoreWhitespace = true });

带有参数的构造方法:

class C {
    C(params int[] p) {}
}

C c = new(1, 2, 3);

调用方法时:

class A {}
static void M(A a) {};

M(a: new());

配合对象初始化器:

X x = new() { field = 42 };

泛型的类型推导,需要注意,要有一个显示类型声明才能正确推导:

void M<T>(T t1, T t2) {}

M(new X(), new());

类似的,数组的声明

var arr = new[] {new X(), new()};

不适用此特性的场景

值类型的初始化,可以使用 default 替代

int x = new(); // ERROR
Struct y = new(); // ERROR

使用 as 操作时无法正确推导

Console.Write(new() as X); // ERROR

自然,使用 var 时也无法推导

var x = new(); // ERROR

有歧义的重载

void M(object a, X b) => Console.Write($"{a} {b}");
void M(X a, object b) => Console.Write($"{a} {b}");

M(new(), new()); // ERROR

需要注意的地方

这个新语法 new(),比较容易与匿名类型语法混淆 new{},它们两个是完全不同的东西,需要注意一下。

参考

[Proposal: Target-typed new expressions]
[Unit test: TargetTypedNewTests.cs]