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

前端 未结 25 1933
别那么骄傲
别那么骄傲 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条回答
  •  Happy的楠姐
    2020-11-29 22:40

    Null is an extremely powerful feature. What do you do if you have an absence of a value? It's NULL!

    One school of thought is to never return null, another is to always. For example, some say you should return a valid but empty object.

    I prefer null as to me it's a truer indication of what it actually is. If I can't retrieve an entity from my persistence layer, I want null. I don't want some empty value. But that's me.

    It is especially handy with primitives. For example, if I have true or false, but it's used on a security form, where a permission can be Allow, Deny, or not set. Well, I want that not set to be null. So I can use bool?

    There are a lot more I could go on about, but I will leave it there.

提交回复
热议问题