[C # advanced series] 18. null value type,

Source: Internet
Author: User

[C # advanced series] 18. null value type,

It can be a NULL value type, as literally, it can be a NULL value type.

The meaning of this item can be solved. For example, the Int value of the database can be NUll, making it easier to process database data.

In fact, the null value type is Nullable <T>, while C # has a simpler Syntax: int? This usage:

Nullable <Int32> database type = null; float? Can be empty floating point type = null; DateTime? More value types = null;

More Play modes with null values

In most cases, C # can be used to operate on the null type. It can be regarded as a null type? .

Some examples are as follows:

Int32? Can be empty type = 5; // value type can be implicitly converted to an empty type int value type = (Int32) can be empty type; // You can forcibly convert an empty type to a value type. // You can also convert a controllable type to a value type by using the following two methods. value; // This method is generally acceptable, but when the Value of the null type is null, an exception Value type = can be null. getValueOrDefault (123); // obtain the value of the null type if it is not null. If it is null, the value of the specified number is 123. If no parameter is specified, the value is the default value of the returned type. // The value type can also be used for the null type of the operator ++; // For The unary operator, if the null type value is null, the result is null, if the value is not null, the result is the same as the normal value type. The value type = the null type + the value type. // For binary operators, if one of the two operands is null, the result is null, if none of them are null, the result is the same as the normal value type. // a special case is & | used in Boolean? When the operand is used. // For & Operations, if one operation is false and the result is false, if none is false, then null is null, And the last case is true // For | operation, if one of them is true, the result is true, and none of them are true, then null is null, And the last case is false.

Note that an instance of the null type will generate a large amount of code, even if it is just a simple a + B, there will be a lot of code.

When you use this item, you can imagine that you will first go to the new Nullable <T> instance, and before the operation, you will determine whether it is null, and then determine whether it is successful to operate the Instance value. Therefore, the speed is certainly slower than the normal value type.

Null join operator of C #

That is ?? Operator. If ?? If the left operand is not null, this number is returned. Otherwise, the right operand is returned.

For the null type, this effect is the same as that of the GetValueOrDefault () parameter.

However, it is not only used for the null type, but also for the reference type.

Value Type = NULL type ?? 123; String reference type = GetSomeString ()?? "Troy said: This is an empty text ";

Packing and unpacking of null values in CLR

As mentioned above, the null type is actually a value type, so there are still problems with packing and unpacking.

However, CLR executes some special code for the packing and unpacking of null values:

If the value is null, null is directly transferred to the reference type without packing. If it is not null, the value is taken and the value is packed.

It is also very easy to split the box with a null value. If the reference type is null, the value is directly null. Otherwise, it follows the normal unpacking logic.

Special processing of the null type by CLR

The value type returned by GetType is the type of its value rather than the actual type. In fact, what we want to get when we play this game is of course the value type, rather than the Nullable <T> type. So the CLR does this here.

Console. WriteLine (null type. GetType (); // System. Int32 is returned instead of System. Nullable <Int32>

Call the interface method with the Null Value Type

public struct Nullable<T> where T : struct

The above is the definition of Nullable <T>. We can see that it does not inherit any interfaces.

However, it can call the interface method implemented by value type T:

Int32 result = (IComparable) can be empty type ). compareTo (5); // The allowed method is equivalent to the following method. It only means that CLR has simplified the processing result = (IComparable) (Int32) can be null type ). compareTo (5 );

 

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.