Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a simple class like this.

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

Strangely I get the following warning.

Severity    Code    Description Project File    Line    Suppression State
Warning CS8618  Non-nullable property 'From' must contain a non-null value when exiting constructor. 
Consider declaring the property as nullable.    MxWork.Elsa2Wf.Tuts.BasicActivities  
D:\work\MxWork\Elsa2.0WfLearning\MxWork.Elsa2.0Wf.Tuts\src 
\MxWork.Elsa2Wf.Tuts.BasicActivities\Messages\Greeting.cs   5   Active

I am baffled. These new kind of messages that it throws pulls down my confidence. I got them from all the three properties. And this has suddenly appeared.

Can some one please suggest how this can be mitigated.

visual studio warning message

Update

These days I have seen using default! like so, and its working.

public class Greeting
{
    public string From { get; set; } = default!;
    public string To { get; set; } = default!;
    public string Message { get; set; } = default!;
}

Also you may put a question mark symbol(?) to indicate that the type is nullable, if you feel appropriate as follows.

public class Greeting
{
    public string? From { get; set; };
    public string? To { get; set; };
    public string? Message { get; set; };
}
like image 879
VivekDev Avatar asked May 12 '21 14:05

VivekDev


People also ask

What is non-nullable property?

Non-nullable property 'propername' must contain a non-nullvalue when exiting constructor. Consider declaring the property as nullable. After carefully observing this error message, it makes sense for those properties. In order to minimize the likelihood that our code causes the runtime to throw System.

Could not be set to a null value you must set this property to a non null value of type system Int32?

You must set this property to a non-null value of type 'Int32'." In your EDMX, if you go under your Y table and click on X column, right-click, click on Properties, scroll down to Nullable and change from False to True .

What is non-nullable?

Nullable variables may either contain a valid value or they may not — in the latter case they are considered to be nil . Non-nullable variables must always contain a value and cannot be nil . In Oxygene (as in C# and Java), the default nullability of a variable is determined by its type.

What is a nullable property?

You typically use a nullable value type when you need to represent the undefined value of an underlying value type. For example, a Boolean, or bool , variable can only be either true or false . However, in some applications a variable value can be undefined or missing.

Is non-nullable property required when exiting constructor?

Non-nullable property must contain a non-null value when exiting constructor. Consider declaring the property as nullable. Recently, I was getting this warning for most of the properties which are not specified as nullable.

What is nonnon-nullable property in Java?

Non-nullable property must contain a non-null value when exiting constructor. Consider declaring the property as nullable Prior to .NET 6, null-state analysis and variable annotations are disabled for existing project, however, it is enabled for all .NET 6 projects by default.

What is cs8618 non-nullable property error?

Warning CS8618 Non-nullable property 'Enquiries' must contain a non-null value when exiting constructor. Consider declaring the property as nullable. CarRental D:\Git Source Code emp\Release1 emp_1\CarRental\DB\CarRentalContext.cs 14 Active Warning CS8618 Non-nullable property 'Fleets' must contain a non-null value when exiting constructor.

Why all the properties or models are showing warning for non-nullable elements?

Because of which, all the properties or models are showing warning for non-nullable elements. Recent version of the Visual Studio 2022 and .NET 6, we have a common warning for the models or properties. The warning is given below. Non-nullable property must contain a non-null value when exiting constructor.


4 Answers

If you don't want this, you can disable this by deleting the below line from the csproj file or setting it as disable. By default value is disable.

<Nullable>enable</Nullable>

Here is the official documentation.

like image 167
vivek nuna Avatar answered Oct 20 '22 21:10

vivek nuna


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.

like image 28
Slate Avatar answered Oct 20 '22 22:10

Slate


You can annotate a property directly as non-nullable.

public string Property{ get; set; } = null!;

And it will give a warning if user tries to set the Property as null

like image 30
Simon Lehmann Avatar answered Oct 20 '22 23:10

Simon Lehmann


For Entity Framework Working with nullable reference types:

public class NullableReferenceTypesContext : DbContext {
    public DbSet<Customer> Customers => Set<Customer>();
    public DbSet<Order> Orders => Set<Order>();
}
like image 15
Zachary Scott Avatar answered Oct 20 '22 22:10

Zachary Scott