-
Having a required init property which sets a backing field still gives a null warning. The below code gives a warning: Warning CS8618 Non-nullable field '_name' must contain a non-null value when exiting constructor. Consider declaring the field as nullable. public class TestRequiredInit
{
private readonly string _name;
public required string Name
{
get => _name;
init => _name = value;
}
} I don't see a way to create Doing this with constructor instead of required init doesn't produce a warning. public class TestConstructor
{
private readonly string _name;
public TestConstructor(string name)
{
_name = name;
}
public string Name
{
get => _name;
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
As it currently stands, this behavior is expected. However, we will be discussing a possible improvement to the scenario in LDM on Wednesday: #6754. |
Beta Was this translation helpful? Give feedback.
As it currently stands, this behavior is expected. However, we will be discussing a possible improvement to the scenario in LDM on Wednesday: #6754.