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

推荐订阅源

C
Check Point Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
T
The Exploit Database - CXSecurity.com
I
Intezer
P
Privacy & Cybersecurity Law Blog
C
CERT Recently Published Vulnerability Notes
T
Tor Project blog
K
Kaspersky official blog
AWS News Blog
AWS News Blog
Schneier on Security
Schneier on Security
雷峰网
雷峰网
www.infosecurity-magazine.com
www.infosecurity-magazine.com
宝玉的分享
宝玉的分享
G
Google Developers Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Forbes - Security
Forbes - Security
T
The Blog of Author Tim Ferriss
S
Security @ Cisco Blogs
NISL@THU
NISL@THU
N
News and Events Feed by Topic
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
爱范儿
爱范儿
GbyAI
GbyAI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
Blog — PlanetScale
Blog — PlanetScale
Help Net Security
Help Net Security
F
Full Disclosure
V
Vulnerabilities – Threatpost
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
A
Arctic Wolf
D
Docker
T
Tailwind CSS Blog
L
LangChain Blog
The Last Watchdog
The Last Watchdog
美团技术团队
博客园 - Franky
H
Hacker News: Front Page
Stack Overflow Blog
Stack Overflow Blog
W
WeLiveSecurity
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Recorded Future
Recorded Future
V
Visual Studio Blog
N
Netflix TechBlog - Medium
Hacker News: Ask HN
Hacker News: Ask HN
博客园 - 司徒正美
Cyberwarzone
Cyberwarzone
S
Schneier on Security
Know Your Adversary
Know Your Adversary

博客园 - 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 新特性预览 - 类型推导的 new - Rwing [翻译] 使用 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 新特性预览 - 空参数校验
Rwing · 2020-05-12 · via 博客园 - Rwing

C# 9.0 新特性预览 - 空参数校验

[2020年9月10日更新:此特性已移入下一版本,将不在 C# 9.0 中提供]

前言

随着 .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 新特性预览 - 其他小的变化]

简便的空参数校验 (Simplified Null Argument Checking)

目的

这个特性主要是为了更简便的检查方法的参数是否为 null 并抛出 ArgumentNullExceptiony 异常。

语法

语法很简单,在参数名后加个叹号即可:

void M(string name!) {
    ...
}

以上代码会被翻译为:

void M(string name) {
    if (name is null) {
        throw new ArgumentNullException(nameof(name));
    }
    ...
}

想必有些同学已经从上面代码看出来了,这个生成的空校验,只是校验参数是否为 null,这也就意味着它无法在值类型上使用,以下代码将报错:

// Error: 无法在值类型参数上使用!操作符
void G<T>(T arg!) where T : struct {

}

当然,可空的值类型是可以的,但是编译器会提示一条警告,提示你在可空类型上进行了空检查:

// Warning: 将显式null检查与可为null的类型结合使用
void M(int? x!) { 
}

类似的,在参数拥有默认值的情况下,也会提示警告

// Warning: 参数 'x' 进行了空检查但是它默认为空
void M(string x! = null) { 
}

构造方法的场景

在构造方法的场景下,空参数校验将发生在任何其他代码的前面,包括:

  • 对其他构造方法的链式调用,即 this() 或 base()
  • 在构造方法内的隐式字段初始化

举个例子:

class C {
    string field = GetString();
    C(string name!): this(name) {
        ...
    }
}

以上代码会大致翻译为以下伪代码:

class C {
    C(string name)
        if (name is null) {
            throw new ArgumentNullException(nameof(name));
        }
        field = GetString();
        :this(name);
        ...
}

Lambda 的场景

这个特性在 lambda 中也可以使用

void G() {
    Func<string, string> s = x! => x;
}

不可以使用的场景

这个特性只能用于有方法体的方法中,也就意味着它不能用于抽象方法、接口、委托和部分方法。
以下代码编译器会报错:

interface C
{
    public int M(string x!);// ERROR
}

不能用于属性。因为属性 setter 中的 value 是隐式的,不会出现在参数列表中,所以此特性不适用于属性。

string FirstName! { get; set; } // ERROR

不能用于 out / ref / in 的参数

public void M(out string x!) {} // ERROR

参考

[Proposal: Simplified Null Argument Checking]
[Unit test: NullCheckedParameterTests.cs]
[LDM-2019-07-10.md]