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
18 Replies
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 valuethen what are the clue for this
how can i solve this nullable issue in that case
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 onno but when i remove the null ?
public string this[int employee_id]
i still have the same issueNever 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 finei 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
It can still be null
Can't assign to null
i will make a null check using the if condation insid the set
Yep
even when i did it like so i still have the same wrnning
The result of
FirstOrDefault()
can be nullok i just want to remove the warnning
Check for null then
aha it cant be checked on the same line i have to sperate the FirstOrDefault() then check for null
I mean, you can do
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
$close
If you have no further questions, please use /close to mark the forum thread as answered