C # Difficult to break (8): Nullable type system.nullable_c# Tutorial

Source: Internet
Author: User

null and void

A null value is used to indicate that the data type is not assigned any value, it is a reference type, void means no type, or no value. The difference between null and void is that void is not at all, and null is an empty box with nothing in it.

A null value can only be assigned to a reference type, where it is noted that string is also a reference type, and that the reference type is called "pointers" in C, that is, the location of the memory space where the variable is stored. Setting the variable to null explicitly sets the reference, and it does not point to any memory location itself;

Assigning a null value to a value type will result in a compilation error.

Void is used for the return of a method value and is not inherently a data type, it is simply used to indicate that there is no data type.

System.Nullable

Null values in C # cannot be assigned to value types, and the value types here include struct. The reason is that a value type cannot contain a reference, and null as a "none" reference, of course, cannot be referenced by a value type. This can cause problems in practical applications if a data int type does not determine its value now. You need to use a nullable type here.

Copy Code code as follows:

System.nullable<int> i = null;
Console.WriteLine (i);

I can declare the int type I to be a null type at this point, and the program running results will see no data displayed
Copy Code code as follows:

System.nullable<int> i = null;
Console.WriteLine (i);

I can declare the int type I to be a null type at this point, and the program running results will see no data displayed

Using GetType () to view its type will throw System.NullReferenceException exception

Copy Code code as follows:

System.nullable<int> i = null;
Console.WriteLine (I.gettype ());


System.nullable<t> can be abbreviated to T?, such as system.nullable<int> can write int? Nullable types are used heavily in ado.net programming.

In addition, instances of the nullable type have HasValue members and value members, where HasValue is the bool type, which indicates whether the instance has a value, and value represents the value of the instance when HasValue has a value. Using value when Hasvaue is false triggers the System.InvalidOperationException exception.
Copy Code code as follows:

Int? i = null;
Console.WriteLine (I.hasvalue);
Console.WriteLine (I.value);

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.