0

What is standard practice with new <Nullable>enable</Nullable> setting for classes with properties that are bound at runtime that you know the value will not be null? Examples:

Strongly typed appsettings.json classes

public class CustomSettings
{
    public CustomObject CustomObject { get; set; }
}
public class CustomObject
{
    public string NonNullProperty { get; set; }
}

With json of:

{
    "CustomSettings": {
        "CustomObject": {
            "NonNullProperty": "Some Value Here"
        }
    }
}

Custom MSBuild Task Properties

public class DependencyCopy : Task
{
    [Required]
    public string StartFolder { get; set; } = default!;
    [Required]
    public string ProjectName { get; set; } = default!;
    [Required]
    public string Configuration { get; set; } = default!;
    [Required]
    public string OutputFolder { get; set; }
}

With *.csproj settings of:

<DependencyCopy StartFolder="$(MSBuildProjectDirectory)" ProjectName="$(ProjectName)" Configuration="$(Configuration)" OutputFolder="$(OutputPath)" />

Should I be using #nullable disable and #nullable restore? Settings properties to = default!?

Terry
  • 2,148
  • 2
  • 32
  • 53
  • See also (possible duplicate): https://stackoverflow.com/questions/60812587/c-sharp-non-nullable-field-lateinit I'd use `default!` or `null!`here. – Sweeper Apr 02 '22 at 15:37

0 Answers0