This is an unexpected bug in my code. I didn't realize that C#'s nullish operator isn't a null chaining operator.

In JavaScript, this code is correct:

(colorChip?.ranks.Length ?? 0)

In C# it's wrong, since it evaluates to

((colorChip?.ranks).Length ?? 0)

Instead of nulling out the whole chain. Thus here, if `colorChip` is null, I get a null pointer error.

I can't understand why C# uses this approach. I don't see a situation in which it's beneficial.

0

If you have a fediverse account, you can quote this note from your own instance. Search https://peoplemaking.games/users/mortoray/statuses/115818919188048442 on your instance and quote it. (Note that quoting is not supported in Mastodon.)