Can be empty value type (Nullable) need to note

Source: Internet
Author: User

Possible null value type (nullable <t>)
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.

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 this line of code B will throw an nullreferenceexception when 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:
// X = (B. hasvalue )? B. value: 123
Int32 x = B ?? 123;
Console. writeline (x); // "123"
 
// The line below is equivalent:
// 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 ";

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.