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
  • 140
  • 7
  • 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

3 Answers3

3

Try

public string Code {get;set;} = null!;
gsharp
  • 26,016
  • 21
  • 83
  • 126
  • 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 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,182
  • 12
  • 17
  • 28
rbdeenk
  • 153
  • 1
  • 6
0

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
  • 128,537
  • 12
  • 134
  • 188