Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to tell the compiler that it's ok to have an uninitialized non nullable property

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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) ?

>Solution :

Try

public string Code {get;set;} = null!;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading