Nullable value type (after type with question mark)

Source: Internet
Author: User

Many times you often encounter types with a question mark behind them? such as int? I=null, don't know what to mark, in fact this is the new syntax of 2.0: controllable type.

Because a value type variable can never be null, and a column in the database is allowed to be empty, theCLR introduces a nullable value type in order to correspond to columns in the database.

In C #, a nullable value type is declared and initialized with a question mark notation, as shown in the following code:

Int? X=5;int? Y=null;

C # allows data conversion and transformation of nullable value types, as shown in the following example code:

Int? X=5;int z = (int) x;

Nullable value types can be boxed. The rule is that if a nullable value type is null, theCLR does not make a boxing operation and returns a null value, which is boxed if it is not null. The sample code is as follows:

Static voidNulltoobject () {int? b =NULL; Object o=b; Console.WriteLine ("o is null={0}", o==NULL);//results O is null=tureb=5; o=b; Console.WriteLine ("o ' s type={0}", O.gettype ());//results o ' s type=system.int32}

In this example, if the first result O is null=true, then call O.gettype () immediately will be an error.

You can unboxing a nullable value type. The CLR allows a boxed value type T to be disassembled to a t or a nullable<t>, and the sample code is as follows:

Static void  =5int? a= (int?  ) int b= (int) o; Console.WriteLine ("a={0},b={1}", A, b);  // result a=5,b=5 o=null; a= (int?) o;}

if b= (int) o is added below line eighth, then a null reference exception error is reported.

By calling the GetType () method with a nullable value type, you will find that the resulting result is a System.Int32 type, not a systems.nullable<int32> type.

When an interface method is called with a nullable value type,nullable does not implement the IComparable interface like Int32, but the CLR allows the following code to be compiled.

Static void Nulluseinterface () {int? a=5; int b= ((IComparable) a). CompareTo (5// can be compiled by Console.WriteLine (b);   The result is 0}

The single question mark---is used to set the initial value of the variable, assigning the variable (int type) to NULL instead of 0!

Indicates that the variable can be null, such as: int? I=null, without question mark int i=0; (Cannot let I=null)
public int? Para = Null;//public int para; If you do not comment out the line and comment out the previous line, the following will be an error!public int par () {    

In addition, we may encounter double question marks.

The double question mark---is used to determine and assign a value, to determine whether the variable before the question mark is null, or to assign the value following the question mark to the variable, otherwise assign the value of the preceding variable to the variable.

 Public Static intGetsum (int? D1,int?D2) {         intTemp1 = D1??1; intTemp2 = D2??2; returnTemp1 +Temp2; }Static voidMain (string[] args) {Console.WriteLine (Getsum (NULL,NULL)); Console.WriteLine (Getsum (Ten, -); The result of the operation:3     -            

The nullable value types are described in detail :

For a type, if you can assign a value to it, you can assign it a nullnothingnullptrnull reference (nothing in Visual Basic) (indicating that there is no value), and we say that the type is nullable. Therefore, a nullable type can represent a value, or it indicates that no value exists. For example, a String-like reference type is a nullable type, and a value type similar to Int32 is not a nullable type. Because a value type has only enough capacity to represent a value that is appropriate for that type, it cannot be empty, and the value type has no additional capacity required to represent a null value.

The Nullable class provides supplemental support for the Nullable < (< (t>) >) structure. The Nullable class supports obtaining the underlying type of a nullable type, which supports comparison and equality comparisons for a nullable type whose underlying value type does not support a generic comparison and equality comparison operation.

Scheme
A nullable type is used to represent something that exists or does not exist, depending on the environment. For example, an optional attribute of an HTML tag might exist in one tag, but not in another, or a nullable column of a database table might exist in one row of the table, but not in another row.

You can represent this property or column as a field in a class, and you can define the field as a value type. The field can contain all valid values for a property or column, but cannot provide a value added to indicate that the property or column does not exist. In this case, the field is defined as a nullable type instead of a value type.

Boxing and unboxing
When a nullable type is boxed, the common language runtime automatically boxed the underlying value of the Nullable < (of < (t>) >) object (not the Nullable < (t>) <) object itself. That is, if the HasValue property is true, the contents of the Value property are boxed. If the HasValue property is False, the Nullnothingnullptrnull reference (Nothing in Visual Basic) is boxed. When the underlying value of a nullable type is unboxed, the common language runtime creates a new Nullable < (of < (t>) >) structure initialized to the underlying value.

Nullable value type (after type with question mark)

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.