KelTi
KelTi
CC#
Created by KelTi on 6/9/2024 in #help
"Non-nullable field..." warning even though the value is ensured to be declared
What would be the "industry standard" in that situation? Do I initialize the _empName as String.Empty or declare it as nullable with "string?"?
12 replies
CC#
Created by KelTi on 6/9/2024 in #help
"Non-nullable field..." warning even though the value is ensured to be declared
Got it, so the code is safe but the compiler just isn't smart enough to figure that out. Thought so. Thank you for your answer
12 replies
CC#
Created by KelTi on 6/9/2024 in #help
"Non-nullable field..." warning even though the value is ensured to be declared
Here's the final constructor:
c#
public Employee(string name, int id, float pay, int age)
{
Name = name;
ID = id;
Pay = pay;
Age = age;
}
c#
public Employee(string name, int id, float pay, int age)
{
Name = name;
ID = id;
Pay = pay;
Age = age;
}
12 replies
CC#
Created by KelTi on 6/9/2024 in #help
"Non-nullable field..." warning even though the value is ensured to be declared
I skipped some code for brevity. That's what all of my constructors look like:
c#
public Employee() : this("", -1, 0, 0) { }

public Employee(string name, int id, float pay) : this(name, id, pay, 0) { }
c#
public Employee() : this("", -1, 0, 0) { }

public Employee(string name, int id, float pay) : this(name, id, pay, 0) { }
So you can see, it will be some string value in every case, even if the user opts for the default constructor. The other variables are irrelevant in this case
12 replies