I'm wondering about the correct way to deal with a warning I'm see in VS2022 in a .net 6.0 project. I have the initialization for a class done in a separate method, i.e. not directly in the constructor. But I get a warning saying: Non-nullable field 'Name' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
I don't want the Name field to be nullable, in other words, an instance of this class should always have a value for Name. And the Init() method gets called from both constructors and sets this value. If it helps, the warning number is CS8618.
There seems to be more than one solution to this, but what is the correct way to get rid of this warning ? I'd like to avoid having #pragma ignore warning ugliness in my code, so I wouldn't consider this a valid solution for me.
class ClassA {
public string Name;
public ClassA(int i) {
Init(i.ToString());
}
public ClassA(string s) {
Init(s);
}
private void Init(string id) {
Name = id;
//lots of other stuff happening in this method
}
}
CodePudding user response:
You could also use a get-only-property
for your property.
public class ClassA
{
public string Name { get; }
public ClassA(int i)
{
Name = i.ToString();
}
public ClassA(string s)
{
Name = s ?? string.Empty;
}
}
CodePudding user response:
You should not ignore nullability warnings whenever it is possible. In this particular case you can use
and according to Reference types (C# Reference)
C# also provides the following built-in reference types:
- dynamic
- object
- string
In .NET Framework 4.8, the following is permissible:
public string Name { get; set; } = null;
However, in .NET 6, this results in the following warning: warning CS8625: Cannot convert null literal to non-nullable reference type.
In .NET 6, one needs to specify a default (non-null) value - String.Empty can be used.
public string Name { get; set; } = string.Empty;
In .NET 6, if one would like to allow a null value for a String
, do the following:
public string? Name { get; set; } = null;
Resources: