[英]Why am I getting warning that a non-nullable field must be initialised (When I'm sure I'm initialising it)?
I have:我有:
public abstract class Craft
{
public Craft()
{
CurrentQuadrant = new Point(0, 0);
}
private Point _currentQuadrant;
public Point CurrentQuadrant
{
get => _currentQuadrant;
set
{
_currentQuadrant = value;
}
}
}
(Point is a simple xy pair). (点是一个简单的 xy 对)。
Why would that be giving me the warning that Non-nullable field '_currentQuadrant' must contain a non-null value when exiting the constructor
?为什么会
Non-nullable field '_currentQuadrant' must contain a non-null value when exiting the constructor
? Doesn't the assignment make sure it's non-null?分配不是确保它是非空的吗?
Too little reputation to comment, so I'll post it here.名声太小,无法评论,所以我会在这里发布。
If you're using .NET 5+, you can use the [MemberNotNull]
attribute (more info here ).如果您使用的是 .NET 5+,则可以使用
[MemberNotNull]
属性(更多信息在这里)。
Just put [MemberNotNull(nameof(_currentQuadrant)]
on your constructor and the warning will disappear.只需将
[MemberNotNull(nameof(_currentQuadrant)]
放在您的构造函数上,警告就会消失。
Or you could just assign the value to _currentQuadrant
instead of CurrentQuadrant
.或者您可以将值分配给
_currentQuadrant
而不是CurrentQuadrant
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.