0

i have enabled Nullable Reference Types in my old project and i'm new to this concept.

i have a data model like this :

 public class AddClientInput
    {
        public string Code { get; set; }
        public string Name{ get; set; }
    }

this compiler show this warning message :

Warning CS8618 Non-nullable property 'Name' must contain a non-null value when exiting constructor.

in typescript i can supress the warning by adding the "null forgiving operator"

 class AddClientInput
    {
       Code!: string;
       Name!: string;
    }

How can i do something similar to this in c# (without initializing the variable) ?

CSharp-n
  • 291
  • 2
  • 15
  • 1
    I think youve maybe misunderstood the point of nullable ref types. With it on, a reference type (ie, `string`) not marked nullable with `?` must be initialised – Jamiec Apr 25 '22 at 08:39
  • Possible duplicate of [Suppress a warning for all projects in Visual Studio](https://stackoverflow.com/questions/50399422/suppress-a-warning-for-all-projects-in-visual-studio) – Kent Kostelac Apr 25 '22 at 08:53

4 Answers4

3

Try

public string Code {get;set;} = null!;
gsharp
  • 27,557
  • 22
  • 88
  • 134
  • nullable reference types - if you want a string to be nullable it needs to be `string?` – Jamiec Apr 25 '22 at 08:37
  • That's not how I understand the question. It should be non nullable and suppress the warning. Then that's the solution. – gsharp Apr 25 '22 at 08:38
  • _"i have enabled Nullable Reference Types in my old project"_ line 1 – Jamiec Apr 25 '22 at 08:39
  • "How to tell the compiler that it's ok to have an uninitialized non nullable property" :-) – gsharp Apr 25 '22 at 08:39
  • Fine then your type must be declared as `string?` in your answer then it makes sense. But you cant initialiser a non-nullable string to null. – Jamiec Apr 25 '22 at 08:40
  • My code is not initializing anything, it's the way to suppress the error warning. That's that was asked. – gsharp Apr 25 '22 at 08:43
  • Absolutely you can initialize a non-nullable string (or other reference type) to null, you just have to tell the compiler to shut up about it, which is what this answer shows how to do. Personally I don't think this is a good idea to do, because you're sort of removing value from the whole concept, but this is what was asked. – Lasse V. Karlsen Apr 25 '22 at 09:17
  • @LasseV.Karlsen, yes i believe it's a bad idea too , but what's your solution if you want to create just a data model class or an entity framework model... ? – CSharp-n Apr 25 '22 at 12:48
1

You have told the compiler that this property will never be null. So you must initialise it. I usually go with an empty string for string types in this case.

public string Code { get; set; } = string.Empty;

If you want the string to be nullable you must declare it as such

public string? Code { get; set; } // Compiler wont complain now
Jamiec
  • 133,658
  • 13
  • 134
  • 193
1

You could make the property nullable by adding a ?

Public string? Code { get; set; }

On the other hand you could

Public string Code { get; set; } = default;

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>
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Roe
  • 633
  • 2
  • 14
1

It depends on what behavior you want to achieve. I would not recommend doing this:

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

Here we say "This property is not supposed to have nulls", and at the same time we say "hey compiler, I know there is an issue with that code (we don't have to provide a value for the property Code when we create an instance of that type), but please close your eyes on that".

A better way would be either records with positional syntax:

public record AddClientInput(string Code, string Name);

or if it's a class that can't be converted into a record, then a constructor:

public record AddClientInput
{
    public string Code{ get; set; }
    public string Name{ get; set; }

    public AddClientInput(string code, string name)
    {
        Code = name;
        Name = email;
    }
}

or required keyword introduced in c#11:

public class AddClientInput
{
    public required string Code { get; set; }
    public required string Name{ get; set; }
}
powerz
  • 31
  • 3