3 ms·
I'm on the fence about this. From the example, `myObject?.Items?.[0]?.DoSomething()`. If myObject, Items or Items[0] returns null, what do I do next? Null chec
by acron0 12y ago
I'm on the fence about this. From the example, `myObject?.Items?.[0]?.DoSomething()`.
If myObject, Items or Items[0] returns null, what do I do next? Null check to find out which one?
- jrajav 12y agoIt's often the case that if something along the way is null, breaking the reference chain, it's acceptable to treat it as if that last value were null. E.g., looking up a preference that enables some optional behavior on a module, but the module itself is also optional and might not be loaded. I really can't even count the number of times that I've done a series of null checks on a reference chain just to do one thing if I reached the final value and another thing if I never got there or got null - It's pretty rare that I'll care about an intermediate null, and if I do I've usually checked it earlier.
- darylteo 12y agoIf you need to know which one returns null, then do it the standard way.