Dereference of a possibly null reference [Answered]
I'm making a HTTP call and I get the aforementioned warning (also see attached). I thought my if-else check would handle this. Any guidance? Thanks in advance!
9 Replies
yes jsonResponse itself may not be null in this case but PageData is possible it can have 0 elements
so is best practice to check PageData.Count > 0?
yes
if you are sure PageData.Count will never be 0
just use First() instead
or .FirstOrDefault()! to tell the compiler that this will never return null to shut off the warning
I went with adding
and then also used .First() on the else return.
Thanks for the help! 🙂
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
My lazy butt would likely just do
don't use
Count() == 0
the compiler should suggest using !PageData.Any()
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
✅ This post has been marked as answered!