Non-Nullable Property Must Contain a Non-Null Value When Exiting Constructor. Consider Declaring the Property as Nullable

Non-nullable property must contain a non-null value when exiting constructor. Consider declaring the property as nullable

The compiler is warning you that the default assignment of your string property (which is null) doesn't match its stated type (which is non-null string).

This is emitted when nullable reference types are switched on, which changes all reference types to be non-null, unless stated otherwise with a ?.

For example, your code could be changed to

public class Greeting
{
public string? From { get; set; }
public string? To { get; set; }
public string? Message { get; set; }
}

to declare the properties as nullable strings, or you could give the properties defaults in-line or in the constructor:

public class Greeting
{
public string From { get; set; } = string.Empty;
public string To { get; set; } = string.Empty;
public string Message { get; set; } = string.Empty;
}

if you wish to retain the properties' types as non-null.

Non-Nullable Property Must Contain A Non-Null Value

I'm assuming you have nullable reference types enabled. There's a number of ways you can deal with this.

  1. Explicitly initialize those properties to the default or a known value either at the property declaration directly or within a constructor.
    public class SomeType
    {
    public string SomeProperty { get; set; } = default!;
    }
  2. Disable nullable reference types for the entire file or section.
    #nullable disable // at the top of the file

    #nullable restore // after the block of code you wanted to temporarily disable
  3. Disable nullable reference types for the entire project.

    Remove or change the <Nullable> setting in your project file. (It defaults to enabled in .NET 6)

I would try to stick to #1 exclusively. Leave #3 alone if the intention is to make the transition.

C#10 nullable pattern: how to tell the compiler I set the non-nullable property in the constructor indirectly?

Use MemberNotNullAttribute to mark your function:

using System.Diagnostics.CodeAnalysis;

class Test
{

string S { get; set; }

public Test()
{
Init();
}

[MemberNotNull(nameof(S))]
private void Init()
{
S = "hello";
}

}

The compiler will now complain if you do not initialize S in Init:

Sample Image

See more scenarios in this article: Attributes for null-state static analysis

Non-nullable warning with EF core DbSet

I've just fixed it like this. Need to make the property read only.

public class MyContext : DbContext, IAudit
{
public MyContext(DbContextOptions options) : base(options) { }

public DbSet<Audit> Audit => Set<Audit>()
}

public interface IAudit
{
DbSet<Audit> Audit { get; }
}

What is best way to create .NET6 class with many non-nullable properties?

Since C#11 you can add a Required modifier.

public class Meeting
{
public required Name Name { get; set; }
public required Person ResponsiblePerson { get; set; }
public required Description Description {get; set; }
public required Category Category { get; set; }
public required Type Type { get; set; }
public required DateTime StartDate { get; set; }
public required DateTime EndDate { get; set; }
public List<Person> Attendees { get; set; } = new List<Person>();
}

This feature aims to improve how we initialize objects that do not rely on constructor parameters.

More detailed info:
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/required



Related Topics



Leave a reply



Submit