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
Angius5mo 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 preadlyOP5mo ago
then what are the clue for this how can i solve this nullable issue in that case
Angius
Angius5mo 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 preadlyOP5mo ago
no but when i remove the null ? public string this[int employee_id] i still have the same issue
Angius
Angius5mo 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 preadlyOP5mo 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
Angius5mo ago
It can still be null Can't assign to null
steven preadly
steven preadlyOP5mo ago
i will make a null check using the if condation insid the set
Angius
Angius5mo ago
Yep
steven preadly
steven preadlyOP5mo 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
Angius5mo ago
The result of FirstOrDefault() can be null
steven preadly
steven preadlyOP5mo ago
ok i just want to remove the warnning
Angius
Angius5mo 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 preadlyOP5mo ago
aha it cant be checked on the same line i have to sperate the FirstOrDefault() then check for null
Angius
Angius5mo 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 preadlyOP5mo 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
Angius5mo ago
$close
MODiX
MODiX5mo 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