Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nullable reference type in C#8 when using DTO classes with an ORM

I activated this feature in a project having data transfer object (DTO) classes, as given below:

public class Connection     {         public string ServiceUrl { get; set; }         public string? UserName { get; set; }         public string? Password { get; set; }         //... others      } 

But I get the error:

CS8618: Non-nullable property 'ServiceUrl' is uninitialized. Consider declaring the property as nullable.

This is a DTO class, so I'm not initializing the properties. This will be the responsibility of the code initializing the class to ensure that the properties are non-null.

For example, the caller can do:

var connection = new Connection {   ServiceUrl=some_value,   //... } 

My question: How to handle such errors in DTO classes when C#8's nullability context is enabled?

like image 612
M.Hassan Avatar asked Nov 30 '19 16:11

M.Hassan


People also ask

What is a nullable reference type?

Nullable reference types are a compile time feature. That means it's possible for callers to ignore warnings, intentionally use null as an argument to a method expecting a non nullable reference. Library authors should include runtime checks against null argument values.

Is nullable type C#?

In C#, the compiler does not allow you to assign a null value to a variable. So, C# 2.0 provides a special feature to assign a null value to a variable that is known as the Nullable type. The Nullable type allows you to assign a null value to a variable.

IS null value a reference type?

null isn't a type. From msdn: The null keyword is a literal that represents a null reference, one that does not refer to any object. Nullable types are instances of the Nullable<T> struct which "Represents a value type that can be assigned null."

What is nullable and non nullable value type?

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.


2 Answers

You can do either of the following:

  1. EF Core suggests initializing to null! with null-forgiving operator

    public string ServiceUrl { get; set; } = null! ; //or public string ServiceUrl { get; set; } = default! ; 
  2. Using backing field:

    private string _ServiceUrl; public string ServiceUrl {     set => _ServiceUrl = value;     get => _ServiceUrl            ?? throw new InvalidOperationException("Uninitialized property: " + nameof(ServiceUrl)); } 
like image 158
M.Hassan Avatar answered Sep 25 '22 15:09

M.Hassan


If it's non nullable, then what can the compiler do when the object is initialized?

The default value of the string is null, so you will

  1. either need to assign a string default value in the declaration

    public string ServiceUrl { get; set; } = String.Empty;

  2. Or initialize the value in the default constructor so that you will get rid of the warning

  3. Use the ! operator (that you can't use)

  4. Make it nullable as robbpriestley mentioned.

like image 24
Athanasios Kataras Avatar answered Sep 23 '22 15:09

Athanasios Kataras