C # nullable value types

Source: Internet
Author: User
Tags field table

Tag: pos ToString Post Unpacking Field table overload remove false allow

We know that a variable of value type is never null, but a column in the database may allow a value to be null, but there is no way to represent the INT32 value as NULL in the CLR.

To solve this problem, the CLR introduces a nullable value type, in order to understand how it works, first look at the system.nullable<t> structure defined in the FCL.

One, the code of the nullable value type

 Public structNullable<t>whereT:struct   {        //two fields indicate status       PrivateBoolean HasValue =false;//assume null       InternalT value =default(T);//Assuming all the bits are O               PublicNullable (T value) { This. Value =value;  This. HasValue =true; }        PublicBoolean HasValue {Get{returnHasValue;} }        PublicT Value {Get {               if(!hasValue) {                   Throw NewInvalidOperationException ("a Nullable object must have a value. "); }               returnvalue; }       }        PublicT GetValueOrDefault () {returnvalue; }        PublicT GetValueOrDefault (t defaultvalue) {if(!hasvalue)returnDefaultValue; returnvalue; }       //overloaded Equals method        Public Override BOOLEquals (Objectobj) {           if(! HasValue)return(obj = =NULL); if(obj = =NULL)return false; returnvalue.       Equals (obj); }       //overloaded ToString () method        Public Override stringToString () {if(! HasValue)return ""; returnvalue.       ToString (); }        Public Static Implicit operatorNullable<t>(T value) {return NewNullable<t>(value); }        Public Static Explicit operatorT (nullable<t>value) {           returnvalue.       Value; }   }

As you can see, a nullable type is a struct type, which is itself a value type, and the instance is still on the stack, and the type parameter of the nullable is constrained to a struct, meaning that the type is set for the value type.

Second, the use of nullable value types

To use a nullable Int32 in our code, we can write like this:

5 ;        Nullablenull;

But in C #, this is generally the case

int 5 ; int null;

Third, merge operators

C # provides an empty join operator, i.e.?? operator, if the left is not NULL, returns the operand, and if the left is null, the right operand is returned.

The following two lines of code are equivalent

int NULL ; int 123 ; int 123;

Four, empty type of packing and unpacking

Boxing: When the CLR is boxing a nullable type, it checks to see if it is null, and if so, the CLR does not box anything and returns null directly, if the nullable instance does not null,clr the value from a nullable instance and boxing it, that is to say, A nullable type with a value of 5 is boxed into a boxed INT32 with a value of 5.

Unpacking: If the boxed value type refers to null and you want to unboxing it to a nullable type, the CLR sets the value of the nullable type to null.

Five, nullable type call GetType

Calling GetType on a nullable type actually tells you that the type is T, not nullable<t>. That means Int32? x=5; when X.gettype () is executed, System.Int32 is displayed instead of system.nullable<int32>;

C # 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.