. NET Nullable value types

Source: Internet
Author: User

Microsoft introduced the concept of nullable value types (nullable value type) in the CLR.

The system.nullable<t> classes defined in the FCL are as follows:

[Serializable,structlayout (LayoutKind.Sequential)]

public struct nullable<t> where t:struct {

Private Boolean Hasvalue=false;

Internal T Value=default (T);

.................. Slightly

}

First, C # support for nullable value types

    1. Nullable<t> is a value type, and the instance is still "lightweight". That is still available on the stack, the size of the instance is the original value type +the boolean field size.
    2. Int32 is equivalent to nullable<int32>.
    3. The unary operator (++,+,-,--,!, ~) operand is null, and the result is null.
    4. The binary operator (+,-, *,/,%,&,|,^,<<,>>) is null for any of the two operands, and the result is null.

Int32? a=5;

Int32? B=null;

// Unary operator

a++ ; //6;

B=-b; Null

// Two-dollar operator

a=a+3;//a=9;

B=b*3;//b=null;

    1. Equality operator (==,!=) Two operands are null, both are equal; an operand is null and the two are not equal. Two operands are not NULL, compare values to determine whether they are equal.
    2. Relational operator (<,>,<=,>=) Two operands any one of which is null and the result is false. Two operands are not NULL, compare values.

Second, the empty union operator of C #

    1. The Empty Union operator (null-coalescing operator), i.e. ?? Operator.
    2. It is going to get two operands, and adding the operand to the left is not NULL, it returns the value of the operand. If the operand on the left is null, the value of the right operand is returned.
    3. With the empty union operator, the default value of the variable can be easily used. It can have both a reference type and a nullable value type.
    4. ?? Better to use in composite scenarios, such as:

String s= DoThing1 ()?? DoThing2 ()?? "Nothing!";

Three, empty value of boxing , unboxing and call GetType

    1. When the CLR is boxing a nullable<t> instance, it checks to see if it is null.

A) If the CLR is not actually boxed and returns a null value;

b) If the value is not removed from the nullable type for NULL,CLR, and it is boxed.

    1. The CLR allows a boxed value type T to be unboxing to a t or a nullable<t>.

If a reference to a boxed value type is null and is to be disassembled to a nullable<t>, the CLR sets the value of nullable<t> to null.

    1. Int32? x=5;

Console.WriteLine (X.gettype ());//output System.Int32, not system.nullable<int32>.

. NET Nullable value types

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.