Mina Shaker

Mina Shaker

  • 1.6k
  • 21
  • 160

why i am still getting this warning while i am using set accessor ?

May 26 2024 7:32 PM

hello i have this class but iam still having the below warring in visual studio while i am using set accssor to set the name feild 

Severity    Code    Description    Project    File    Line    Suppression State Warning (active)    CS8618    Non-nullable field '_name' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.    C#ForBeginners    D:\Projects\C#ForBeginners\C#ForBeginners\Program.cs    15  `

public class Student
{
    private int _id;

    private string _name;

    private int _PassMark = 35;


    public int ID 
    {
        set 
        {
            if (value < 0) 
            {
                throw new Exception("id feild can be negative value");
            }  

            _id = value;
        }
        get 
        {
            return _id;
        }
    }

    public string Name 
    {
        set
        {
            if (string.IsNullOrEmpty(value))
            {
                _name = "No Name";

            }

            _name = value;
        }
        get 
        {
            return _name;
        }
    }

}

 

 


Answers (2)