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

前端 未结 25 1979
别那么骄傲
别那么骄傲 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条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-29 22:24

    • Explicit nulling, though that is seldom necessary. Perhaps one can view it as a form of defensive programming.
    • Use it (or nullable(T) structure for non-nullables) as a flag to indicate a missing field when mapping fields from one data source (byte stream, database table) to an object. It can get very unwieldy to make a boolean flag for every possible nullable field, and it may be impossible to use sentinel values like -1 or 0 when all values in the range that field is valid. This is especially handy when there are many many fields.

    Whether these are cases of use or abuse is subjective, but I use them sometimes.

提交回复
热议问题