C# 11 新增了 required 关键字:What's new in C# 11: Required members

今天想用上,结果一保存,关键字就没了。

// before
public class Person
{
    public required string FirstName { get; init; }
    public required string LastName { get; init; }
}

// after
public class Person
{
    public string FirstName { get; init; }
    public string LastName { get; init; }
}

第一时间想到是被 CodeMaid 给清掉了,找了下 issue,果然有同样的现象: C# 10 'required' keyword not preserved during cleanup

解决办法就是把 CodeMaid 对属性的自动插入访问符的功能给关了

CodeMaid->Options->Cleaning->Insert->Insert explicit access modifiers on properties

file

看了眼 Releases,CodeMaid 最后的更新日期定格在 2021 年,着实跟不上 C# 的更新速度了 ╮(╯▽╰)╭ 。