Null
NullA keyword is a text value that does not reference any null reference of an object.
- Null is the default value of the reference type variable.
- C #2.0 introducedType that can be nullThis is a data type that can be set to an undefined value. A value that can be null can represent a value in the range of basic values of a general type. When a null value is added, it is equivalent to the union of the common value type and null.
Null typeIs an instance of the System. Nullable structure.
System. Nullable <T>
// Or abbreviated
T?
// The two can be exchanged, and T is the value type.
- The value type variable that can be assigned a null value. An empty type based on the reference type cannot be created.
- If the base type value is null, use the System. Nullable. GetValueOrDefault attribute to return the value or default value of the base type, for example, int j = x. GetValueOrDefault ();
- Use the read-only attributes of HasValue and Value to test whether it is null or retrieve the Value. For example, if (x. HasValue) j = x. Value;
If this variable contains a value, the HasValue attribute returns True; or if the value of this variable is null, False is returned.
If a Value is assigned, the Value Attribute returns this Value. Otherwise, System. InvalidOperationException is thrown.
Set HasValue to false by default for empty type variables. Undefined Value.
Use ?? The default value is assigned to operators. If the null type is assigned to a non-null type, the default value is applied, such as int? X = null; int y = x ?? -1 ;. (For example, in photo album management, if an empty image is uploaded, you can use this formula to assign it a default image)
Nested empty types are not allowed. The following line is not compiled: Nullable <int> n;
True & false
Can be used as operators and identifiers
- If the type defines the true operator, it must also define the false operator.
- The type cannot directly overload the conditional logical operators (& |), but the same effect can be achieved by reloading the rule logical operators and the true and false operators.
Default
DefaultKeywords can be used in switch statements or generic code.
- If no case expression matches the switch value, the control is passed to the statement following the optional default label. If no default tag exists, the control is passed outside the switch.
- Int id = int32.Parse (Console. ReadLine ());
Switch (id)
{
Case 1:
Console. WriteLine ("Lee ");
Break;
Case 2:
Console. WriteLine ("Tang ");
Break;
Default:
Console. WriteLine ("Sorry, no one match this ID! ");
Break;
}
- One problem that occurs in generic classes and generic methods is how to assign the default value to parameterized type T when the following conditions are unknown:
T is the reference type or value type.
If T is of the value type, whether it is a value or a structure.
- Public class GenericClass <T>
{
Public T dosomething (arg)
{
T temp = default (T );If (arg! = Null)
{
Temp = arg. Favs;
}
Return temp;
}
//
}
Given a variable T of the parameterized type t, the Statement T = null is valid only when t is of the reference type; only when T is of the numerical type rather than the structure, statement t = 0 can be used normally. The solution is to use the default keyword, which returns NULL for the reference type and zero for the value type. For the structure, this keyword returns each structure member whose Initialization is zero or empty, depending on whether the structure is a value type or a reference type.
For more information, see the MSDN document. |
All content in this Blog is provided as "the status quo" without any guarantee and no rights are granted. This posting is provided "as is" with no warranties, and confers no rights. |