i dont know why i am still got `CS8602 - Dereference of a possibly null reference.` in that indexer

here is an indexer i still got this warning in the setter i dont know why
c#
public string? this[int employee_id]
{
get => listEmployees?.FirstOrDefault(emp => emp.EmployeeId == employee_id)?.Name ?? "No Name Found";

set => listEmployees.FirstOrDefault(emp => emp.EmployeeId == employee_id).Name = value;
}
c#
public string? this[int employee_id]
{
get => listEmployees?.FirstOrDefault(emp => emp.EmployeeId == employee_id)?.Name ?? "No Name Found";

set => listEmployees.FirstOrDefault(emp => emp.EmployeeId == employee_id).Name = value;
}
18 Replies
Angius
Angius4mo ago
Well, listEmployees is nullable, apparently But you don't treat it as such in the setter You can't call FirstOrDefault() on a null null has no .Name property null cannot be set to a value
steven preadly
steven preadly4mo ago
then what are the clue for this how can i solve this nullable issue in that case
Angius
Angius4mo ago
Does listEmployees need to be nullable, first of all? If it does... well, just check for null in the setter Use null-conditional operator like in the getter And so on
steven preadly
steven preadly4mo ago
no but when i remove the null ? public string this[int employee_id] i still have the same issue
Angius
Angius4mo ago
Never have I mentioned removing it here Though, it seems this property can't actually return null So making it a string instead of string? is fine
steven preadly
steven preadly4mo ago
i did it like this set => listEmployees?.FirstOrDefault(emp => emp.EmployeeId == employee_id).Name = value; but now i have another issue The left-hand side of an assignment must be a variable, property or indexer
Angius
Angius4mo ago
It can still be null Can't assign to null
steven preadly
steven preadly4mo ago
i will make a null check using the if condation insid the set
Angius
Angius4mo ago
Yep
steven preadly
steven preadly4mo ago
even when i did it like so i still have the same wrnning
c#
set
{
if(listEmployees != null)
{
listEmployees.FirstOrDefault(emp=> emp.EmployeeId == employee_id).Name = value;
}
}
c#
set
{
if(listEmployees != null)
{
listEmployees.FirstOrDefault(emp=> emp.EmployeeId == employee_id).Name = value;
}
}
Angius
Angius4mo ago
The result of FirstOrDefault() can be null
steven preadly
steven preadly4mo ago
ok i just want to remove the warnning
Angius
Angius4mo ago
Check for null then
var emp = listEmployees?.FirstOrDefault(...);
if (emp != null)
{
emp.Name = value;
}
var emp = listEmployees?.FirstOrDefault(...);
if (emp != null)
{
emp.Name = value;
}
steven preadly
steven preadly4mo ago
aha it cant be checked on the same line i have to sperate the FirstOrDefault() then check for null
Angius
Angius4mo ago
I mean, you can do
if (listEmployees?.FirstOrDefault(...) is {} emp)
{
emp.Name = value;
}
if (listEmployees?.FirstOrDefault(...) is {} emp)
{
emp.Name = value;
}
steven preadly
steven preadly4mo ago
but as we talked before i noticed that indexer is more complex than methods but i thhink it has its use cases thank you very mush i wan to make this as solved
Angius
Angius4mo ago
$close
MODiX
MODiX4mo ago
If you have no further questions, please use /close to mark the forum thread as answered
Want results from more Discord servers?
Add your server