gpt4 book ai didi

c# - 默认发起成员(member)的结果是什么! C# .net 中的语句?

转载 作者:行者123 更新时间:2023-12-02 01:30:49 24 4
gpt4 key购买 nike

是的,我尝试用谷歌搜索,发现从 null-coalescing 运算符到 default 关键字的一切可能,一切都绕过了(到目前为止)我对这些语句的好奇心:

public record Person
{
public string FirstName { get; init; } = default!;
public string LastName { get; init; } = default!;
};

该片段来自this MS official site .

因此,虽然我可以尝试此代码在运行时记录实例化中的效果,但任何人都可以大致解释一下 default! 语句背后的美丽和魔力吗?

谢谢。

最佳答案

default关键字本身表示使用此类型的默认值,在本例中为 null,因为 string 是引用类型。

! 后缀是 null forgiving operator 。它会抑制有关可能为 null 的值的任何警告。如果此代码中没有它,您将收到以下警告,这是 nullable reference types 的一项功能。 :

CS8625: Cannot convert null literal to non-nullable reference type

关于c# - 默认发起成员(member)的结果是什么! C# .net 中的语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73363205/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com