The tenth chapter is nullable type

Source: Internet
Author: User

1 Introduction of nullable types

The declarative method for a nullable type is to add a question mark "?" above the underlying type.

int ? i;i=ten;

In C #, only value types have nullable types (reference types can take null values), including system-predefined integer types, character types, real types, Boolean types, and various struct types and enumeration types.

2 Generic Structure Nullabletype

2.1 Overview

. NET is how to implement a nullable type? One scenario is to define a new nullable type for each value type, but this is a lot of work and there is no extensibility: The new value types defined by the developer are not eligible for this benefit unless they define value types and nullable types in pairs each time.

. NET class library takes advantage of the power of generics to implement a nullable type for all value types once and for all. A generic structure NULLABLETYPE<T> is defined in system, and the previous section is actually a shorthand for nullable types. Int? Equivalent to nullabletype<int>. Nullabletype<t> 's shorthand way is T?

This shorthand method can be used for both nullable type declarations and for nullable type constructs. The generic struct nullabletype<t> can actually be defined as a prototype of a constructor that is public nullable<t>, so you can use the following code to construct an instance of a nullable type:

int New int ? (5); // i=5; nullable<intnew nullable<int> (ten); // j=10; New Datetime? (DateTime.Now); // Dt=now

However, this approach does not apply to null cases where the parameter type of the Nullabletype<t> constructor is called T. The following code is not valid:

int ? i=newint? (null); // Error

A nullable type is a compound of a value type and null value, but it is essentially a value type. A variable of a nullable type is also the data that directly contains itself, not a reference to the target data. Therefore, a nullable type can itself be used as an underlying type to construct a new nullable type. The following code is legal:

int Ten ; int?? j = I; int??? K = J; Nullable<double3.14; Nullable<Nullbale<double>> y=x;

2.2 Judging and taking values

Given a variable of a nullable type, you know that it is not null, and can be determined by nullabletype<t> common property hasvalue. The property is a Boolean value.

Another public property of the nullabletype<t> value is the type T, which represents the value of the underlying type that the nullable type corresponds to. This property is valid only if the variable is not a null value, so check to see if the value of the property HasValue is true before accessing it. For example, the following code throws an exception:

int NULL ; Console.WriteLine (i.value);

The correct wording is:

if (I.hasvalue)    Console.WriteLine (i.value);

HasValue and value

The tenth chapter is nullable type

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