提问人:Marvin Klein 提问时间:6/26/2023 更新时间:6/26/2023 访问量:137
当某个方法已执行时,如何告诉编译器属性不为 null?
How do I tell the compiler that a property is not null when a certain method has been executed?
问:
在我的基类中考虑这段代码
public MyClass? Input { get; set; }
protected virtual void DoSomething()
{
Input = new();
}
现在,我想重写该方法并修改 Input 属性上的一些属性。
protected override void DoSomething()
{
base.DoSomething();
Input.Name = "Test";
}
现在我收到警告
CS8602 - Dereference of a possibly null reference.
我知道我可以说,这样做不能为零:
Input!.Name = "Test";
但我不想每次都这样做。当函数的基已经执行时,有没有更好的方法可以告诉编译器 Input 不为 null?
答:
4赞
canton7
6/26/2023
#1
您需要 [MemberNotNull(nameof(Input))]
。
这会告知编译器,在修饰方法执行后,命名属性将为非 null。
[MemberNotNull(nameof(Input))]
protected virtual void DoSomething()
{
Input = new();
}
4赞
jraufeisen
6/26/2023
#2
为此,可以使用 MemberNotNull
。
[MemberNotNull(nameof(Input)]
protected virtual void DoSomething()
{
Input = new();
}
评论
Input