C # Difficulty one by one (8): Nullable type System.Nullable

Source: Internet
Author: User
null and void

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

Null values can only be assigned to reference types, and note that string is also a reference type, and reference types are referred to as "pointers" in C, that is, the location of the memory space where the variable is stored. By setting the variable to NULL, the reference is set explicitly, and it does not point to any memory location by itself;

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

Void is used to return a method value, and its nature is not a data type, it is used only to indicate that there is no data type.

System.Nullable

Null values in C # cannot be assigned to value types, where value types include structs. The reason is that a value type cannot contain a reference, and a reference to NULL as a "None" is certainly not referenced by a value type. This can cause problems in the actual application, assuming that a data int type does not really determine its value now. You need to use a nullable type here.

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

You can declare the I of type int as a null type, and the result of the program run will see no data displayed

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

You can declare the I of type int as a null type, and the result of the program run will see no data displayed

Using GetType () to view its type throws a System.NullReferenceException exception

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 extensively in ADO programming.

In addition, instances of the nullable type have HasValue members and value members, where HasValue is a 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 will trigger the System.InvalidOperationException exception.

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

The above is the C # difficulty one by one (8): Nullable type system.nullable content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.