1. What is null?
In general, null indicates the null type, that is, there is nothing, but "nothing" does not mean "nothing ". In fact, null is so important that in JavaScript, Null is regarded as one of the five basic primitive types, working in parallel with Undefined, Boolean, Number, and String. This importance is also manifested in. NET, but it must be clarified that null is not equivalent to 0, "", string. Empty, which are generally "zero" values. On the contrary, null has a real meaning, which is used to identify a state of variable reference. This State indicates that no object instance is referenced, that is, "nothing ", it is neither an Object instance nor a User instance, but an empty reference. In. NET, null indicates that an object reference is invalid. As the default value for referencing type variables, null is for pointers (references). It is an exclusive concept for referencing type variables, indicating a state in which the referenced type variables are declared but not initialized. For example:
object obj = null;
2 Nullable <T> (null type)
For a long time, null is a unique product of the reference type. If you perform the null operation on the value type, an error message is thrown in the compiler. For example:
// Throw the compile-time error int I = null;
As shown in the example, in many cases, as a developer, we prefer to be able to handle it in a uniform way, in addition, it is hoped that the ing of "value" in actual business needs can also be "null. Therefore. NET 2.0, this feature has been applied to the new System. the emergence of Nullable <T> (that is, the null type) is broken. It is easy to implement the following methods to remove the above criticism:
// Nullable <T> solves this problem int? I = null;
You may be surprised that the above example does not have any Nullable shadow. In fact, this is a syntactic sugar of C #. The following code is essentially equivalent:
int? i = null; Nullable<int> i = null;
Obviously, we prefer to implement our code in the first Concise and elegant way, but in essence Nullable <T> and T? They are all the way. The great significance of the null type is that the Nullable <T> type ,.. NET adds "nullability" for the value type. For example, the value of Nullable <Boolean> includes true, false, and null, nullable <Int32> indicates that the integer value can be null.
For the null type, note the following:
- The value type is null.
- Nested empty types are not allowed, such as Nullable <T>.
- Nullable <T> and T? Is equivalent.
- If the GetType method is executed for the null type, T is returned instead of Nullable <T>.
- C # allows conversions and transformations on the null type, for example:
int? a = 100; Int32 b = (Int32)a; a = null;
3 ?? Operator
In actual program development, to effectively avoid exceptions, null determination is a common occurrence, for example, ToString () operations on any object, all necessary null checks should be performed to avoid unnecessary exception prompts. We often implement this:
string objName = obj == null ? string.Empty : obj.ToString();
Because the above implementation is so frequent, a new operator is provided in. NET 3.0 to simplify the null value judgment process. This is :?? Operator. The above process can show more shocking code:
string objName = (obj ?? string.Empty).ToString();
?? Operator, also known as null-coalescing operator. If the left operand is null, return the value of the right operand. If not null, return the value of the left operand. It can be applied to both null and reference types.