我发现了一段用 C# 编写的代码,貌似是 8.0 版。在代码中,在调用方法之前有一个感叹号。这部分代码是什么意思,最重要的是,它的用途是什么?
var foo = Entity!.DoSomething();
Run Code Online (Sandbox Code Playgroud) 我在 C# 9 预览版中遇到了c# 中的“ init ”关键字。它是什么意思,它的应用是什么?
public class Person
{
private readonly string firstName;
private readonly string lastName;
public string FirstName
{
get => firstName;
init => firstName = (value ?? throw new ArgumentNullException(nameof(FirstName)));
}
public string LastName
{
get => lastName;
init => lastName = (value ?? throw new ArgumentNullException(nameof(LastName)));
}
}
Run Code Online (Sandbox Code Playgroud)