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

推荐订阅源

F
Full Disclosure
V
Vulnerabilities – Threatpost
Attack and Defense Labs
Attack and Defense Labs
N
News and Events Feed by Topic
SecWiki News
SecWiki News
S
Security @ Cisco Blogs
Schneier on Security
Schneier on Security
B
Blog
TaoSecurity Blog
TaoSecurity Blog
The Last Watchdog
The Last Watchdog
H
Hacker News: Front Page
Hacker News - Newest:
Hacker News - Newest: "LLM"
博客园_首页
D
Docker
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Y
Y Combinator Blog
W
WeLiveSecurity
N
News and Events Feed by Topic
F
Fortinet All Blogs
PCI Perspectives
PCI Perspectives
WordPress大学
WordPress大学
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Recent Announcements
Recent Announcements
Forbes - Security
Forbes - Security
T
Tailwind CSS Blog
Hacker News: Ask HN
Hacker News: Ask HN
爱范儿
爱范儿
腾讯CDC
Last Week in AI
Last Week in AI
月光博客
月光博客
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed
Help Net Security
Help Net Security
V
V2EX
C
Cyber Attacks, Cyber Crime and Cyber Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
H
Heimdal Security Blog
L
LINUX DO - 最新话题
GbyAI
GbyAI
The Hacker News
The Hacker News
罗磊的独立博客
S
SegmentFault 最新的问题
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - 【当耐特】
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
V2EX - 技术
V2EX - 技术
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
O
OpenAI News
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻

博客园 - 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 新特性预览 - 顶级语句 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 新特性预览 - init-only 属性
Rwing · 2020-10-27 · via 博客园 - Rwing

C# 9.0 新特性预览 - init-only 属性

前言

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

目录

[C# 9.0 新特性预览 - 类型推导的 new]
[C# 9.0 新特性预览 - 空参数校验]
[C# 9.0 新特性预览 - 顶级语句]
[C# 9.0 新特性预览 - init-only 属性]
[C# 9.0 新特性预览 - Record 类型]
[C# 9.0 新特性预览 - 模式匹配的改善]
[C# 9.0 新特性预览 - 源代码生成器]
[C# 9.0 新特性预览 - 其他小的变化]

只初始化 setter (Init Only Setters)

这个特性允许创建只初始化(init only)的属性和索引器,使得 C# 中的不可变模型更加灵活。

背景

在此之前,我们创建实体类/POCO类/DTO类等等模型类的时候,都期望属性只读不允许从外部修改,会将属性的 setter 设为私有或者干脆不设置 setter,例如:

public class Person
{
    public string Name { get; private set; }
    // OR
    //public string Name { get; }
}

再添加一个拥有全部属性作为签名的构造方法:

...
public Person(string name)
{
    this.Name = name;
}
...

这样做虽然可以达到目的,但是带来了两个问题
1.假如属性增多,会带来工作量的成倍增加,以及不易维护
2.无法使用对象初始化器(object initializers)

var person = new Person
{
    Name = "Rwing" // Compile Error 
};

在这个情况下,init 关键字应运而生了。

语法

语法很简单,只需要将属性中的 set 关键字替换为 init 即可:

public string Name { get; init; }

以上代码会被大致翻译为:

private readonly string _name;
public string Name
{
    get { return _name; }
    set { _name = value;}
}

可以看到,与 set 的区别是,init 会为背后的字段添加 readonly 关键字。
这样我们就可以去掉一堆属性的构造方法转而使用对象初始化器了,并且达到了不可变的目的。

var person = new Person
{
    Name = "Rwing"
};
// 初始化后无法再次修改
person.Name = "Foo"; // Error: Name is not settable

这一语法,有很多场景需要配合同样在 C# 9.0 中新增的 record 类型使用。

哪些情况下可以被设置

  • 通过对象初始化器
  • 通过 with 表达式
  • 在自身或者派生类的构造方法中
  • 在标记为 init 的属性中
  • 在特性(attribute)类的命名参数属性中

以上场景不难理解,但是值得一提的是,只有 get 的属性是不可以派生类的构造方法中赋值的,但是 init 可以:

class Base
{
    public bool Foo { get; init; }
    public bool Bar { get; }
}

class Derived : Base
{
    Derived()
    {
        Foo = true;
        Bar = true; // ERROR
    }
}

此外有一种例外, 在以上场景中的 lambda 或本地函数中,也不允许被设置,例如:
原因也很简单,lambda 或本地函数在编译后已经不在构造函数中了。

public class Class
{
    public string Property { get; init; }
    
    Class()
    {
        System.Action a = () =>
        {
            Property = null; // ERROR
        };
        local();
        void local()
        {
            Property = null; // ERROR
        }
    }
}

参考

[Proposal: Init Only Setters]
[InitOnlyMemberTests.cs]