The value type cannot be assigned null, but sometimes we need to set a simple value type to null. For example, a query interface has many query conditions, the query conditions can be used or left blank. This is when the query parameter information collected from the UI is passed to the backend for query. If we specify that a parameter is null, the query parameter is not used.
First, it is a structure type, Value Type
In fact, the following Variable declaration is a value type that can be empty.
Int? Number = 100;
But is number a true value type? We use typeof (Int ?) Look at his type,
System. nullable '1 [system. int32]
The nullable <t> statement is as follows:
Public struct nullable <t> where T: struct
The statement is clear about struct, so we can understand that the original Int? Yes.
For verification, we use number. GetType () to see what the type is. The answer is system. int32.
Therefore, if the value can be null, it is not necessarily a reference type. (Note: CLR itself does not support the value type being assigned null. Each bit can be set to 0 at most. The reason why the value can be assigned to NULL is that the compiler helps us with the conversion, so that the C # syntax supports this situation. This is also the syntactic sugar (Syntactic sugar))
The followingCodeIn the console, the result is 100 instead of 101:
Public void test () {Int? Number = 100; changevalue (number); console. writeline (number);} void changevalue (Int? Number) {number + = 1 ;}
In addition, let's take a look at the following code line:
Int? Number = new nullable <int> ();
The value of the number variable is null.
Some may say that nullable <t> is of the structure type, so packing and unpacking are of the value type. I think this sentence is wrong.
For example:
Object o = NULL; Int? A = (Int ?) O; int B = (INT) O;
A can be split into null values, and the nullreferenceexception will be thrown when this line of code B is running.
Second, nullable <t> can be converted to interface type
Nullabe itself does not implement any interfaces, but please refer to the following code segment:
Int32? N = 5; int32 result = (icomparable) N). compareto (5); // compiles & runs OK console. writeline (result); // 0
This is the benefit that CLR provides to developers.
Without this benefit, we can write the following code to achieve the same process:
Int32 result = (icomparable) (int32) N). compareto (5 );
Third ,?? Operator
By the way, this question ?? (Null-coalescing operator) operator. If the expression on the left of the operator is null, the value on the right of the operator is returned. If the expression value on the left of the operator is not null, the value on the left of the operator is returned.
?? Operators bring a lot of convenience to our coding. Our code is more concise and more readable. Let's look at the following examples:
Private Static void nullcoalescingoperator () {int32? B = NULL; // the line below is equivalent to: // X = (B. hasvalue )? B. Value: 123 int32 x = B ?? 123; console. writeline (x); // "123" // the line below is equivalent to: // string temp = getfilename (); // filename = (temp! = NULL )? Temp: "Untitled"; string filename = getfilename ()?? "Untitled ";}
You can also write:
String S = somemethod1 ()?? Somemethod2 ()?? "Untitled ";
It can also be used in Lamda expressions to enhance readability:
Func <string> F = () => somemethod ()?? "Untitled ";
These things hope to be useful to everyone.
The value type cannot be assigned null, but sometimes we need to set a simple value type to null. For example, a query interface has many query conditions, the query conditions can be used or left blank. This is when the query parameter information collected from the UI is passed to the backend for query. If we specify that a parameter is null, the query parameter is not used.
First, it is a structure type, Value Type
In fact, the following Variable declaration is a value type that can be empty.
Int? Number = 100;
But is number a true value type? We use typeof (Int ?) Look at his type,
System. nullable '1 [system. int32]
The nullable <t> statement is as follows:
Public struct nullable <t> where T: struct
The statement is clear about struct, so we can understand that the original Int? Yes.
For verification, we use number. GetType () to see what the type is. The answer is system. int32.
Therefore, if the value can be null, it is not necessarily a reference type. (Note: CLR itself does not support the value type being assigned null. Each bit can be set to 0 at most. The reason why the value can be assigned to NULL is that the compiler helps us with the conversion, so that the C # syntax supports this situation. This is also the syntactic sugar (Syntactic sugar))
The following code prints 100 instead of 101 in the console:
Public void test () {Int? Number = 100; changevalue (number); console. writeline (number);} void changevalue (Int? Number) {number + = 1 ;}
In addition, let's take a look at the following code line:
Int? Number = new nullable <int> ();
The value of the number variable is null.
Some may say that nullable <t> is of the structure type, so packing and unpacking are of the value type. I think this sentence is wrong.
For example:
Object o = NULL; Int? A = (Int ?) O; int B = (INT) O;
A can be split into null values, and the nullreferenceexception will be thrown when this line of code B is running.
Second, nullable <t> can be converted to interface type
Nullabe itself does not implement any interfaces, but please refer to the following code segment:
Int32? N = 5; int32 result = (icomparable) N). compareto (5); // compiles & runs OK console. writeline (result); // 0
This is the benefit that CLR provides to developers.
Without this benefit, we can write the following code to achieve the same process:
Int32 result = (icomparable) (int32) N). compareto (5 );
Third ,?? Operator
By the way, this question ?? (Null-coalescing operator) operator. If the expression on the left of the operator is null, the value on the right of the operator is returned. If the expression value on the left of the operator is not null, the value on the left of the operator is returned.
?? Operators bring a lot of convenience to our coding. Our code is more concise and more readable. Let's look at the following examples:
Private Static void nullcoalescingoperator () {int32? B = NULL; // the line below is equivalent to: // X = (B. hasvalue )? B. Value: 123 int32 x = B ?? 123; console. writeline (x); // "123" // the line below is equivalent to: // string temp = getfilename (); // filename = (temp! = NULL )? Temp: "Untitled"; string filename = getfilename ()?? "Untitled ";}
You can also write:
String S = somemethod1 ()?? Somemethod2 ()?? "Untitled ";
It can also be used in Lamda expressions to enhance readability:
Func <string> F = () => somemethod ()?? "Untitled ";
These things hope to be useful to everyone.