Why is “null” present in C# and Java?

前端 未结 25 1926
别那么骄傲
别那么骄傲 2020-11-29 22:09

We noticed that lots of bugs in our software developed in C# (or Java) cause a NullReferenceException.

Is there a reason why \"null\" has even been included in the l

25条回答
  •  萌比男神i
    2020-11-29 22:30

    Commonly - NullReferenceException means that some method didn't like what it was handed and returned a null reference, which was later used without checking the reference before use.

    That method could have thown some more detailed exception instead of returning null, which complies with the fail fast mode of thinking.

    Or the method might be returning null as a convenience to you, so that you can write if instead of try and avoid the "overhead" of an exception.

提交回复
热议问题