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.
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; };
}
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.
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 .
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.
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.
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.
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.
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.
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.
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.
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.
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
For Entity Framework Working with nullable reference types:
public class NullableReferenceTypesContext : DbContext {
public DbSet<Customer> Customers => Set<Customer>();
public DbSet<Order> Orders => Set<Order>();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With