В моем классе я устанавливаю значение _owner косвенно в конструкторе через свойство Owner. Однако компилятор по-прежнему считает, что в конце конструктора значение не будет присвоено. Свойство, которое я использовал для назначения резервного поля, уже выполняет нулевую проверку. Почему появляется это предупреждение и как его исправить?
public class BankAccount
{
private string _owner;
// Warning: Non-nullable field must contain a non-null value when
// exiting constructor. Consider declaring the field as nullable
public BankAccount(string owner)
{
// The compiler warning is gone if I check the values here,
// but this leads to code duplication.
this.Owner = owner;
}
public string Owner
{
get
{
return _owner;
}
set
{
if (value is null)
{
throw new ArgumentNullException("Owner name you want to set is null");
}
_owner = value;
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/785 ... value-when
Мобильная версия