"C # Advanced Series" 18 Nullable value types

Source: Internet
Author: User

Nullable value types, as literal, are nullable value types.

The meaning of the existence of this thing can be solved, such as the database of int can be NULL, making it easier to process database data.

In fact, a nullable value type is a generic value type of nullable<t>, and C # has a simpler syntax for the sugar is int? This usage:

NULL ; float NULL ;D Atetime null;

More ways to play with a nullable value type

Most of the time in C # to operate a nullable value type, you can completely consider it as a no? The value type to handle.

Here are some examples of gameplay:

Int32? Nullable type =5;//value types can be implicitly converted to nullable types            intValue type = (Int32) nullable type;//Nullable types can be cast to value types//of course, you can also use the following two ways to convert a controllable type to a value typeValue type = nullable type. Value;//This play is generally possible, but throws an exception when the nullable type value is nullValue type = nullable type. GetValueOrDefault (123);//If the value of the nullable type is not empty, NULL is the specified number 123, the parameter is not specified, and the default value of the value type is returned. //value types are also available for operatorsNullable type + +;//for unary operators, nullable type values are NULL, the result is not NULL, and the result is the same as the normal value typeNullable type = Nullable type + value type;//For a two-tuple operator, there is a null result between the two operands, which is null if neither is null and the normal value type//there is a special case where & and | are applied to a Boolean?            The number of times the operand. //for the & operation, as long as there is a false result of false, there is no false then NULL is NULL, the last case is true//for | operation, as long as one is true then the result is true, there is no true then NULL is NULL, the last case is false

Note the operation of a nullable type instance generates a lot of code, even if it is just a simple a+b.

When you use this thing, you can imagine that you will go to the new nullable<t> instance, and will judge whether it is null before doing the operation, and judge the value of the successful operation of the instance. So its speed will certainly be slower relative to the normal value type.

The empty Join operator for C #

That is?? Operator. If?? The left operand is not NULL, then the number is returned, otherwise the right operand is returned.

For a nullable value type, the effect is the same as the previous getvalueordefault () and specifies the effect of the parameter.

However, it is not just for nullable value types, but also for reference types.

123"Troy said:" This is an empty text ";

Boxing and unpacking of nullable value types in the CLR

The question of boxing and unpacking still exists because the nullable value type is actually a value type.

However, the CLR executes some special code for the boxed and unboxing of nullable values:

A nullable value type can be boxed first to determine whether it is null and NULL to pass NULL to the reference type without boxing. It is not NULL to take its value, and then the value is boxed.

A nullable value type unpacking is also simple, if the reference type is null, the direct assignment is NULL, otherwise follow the normal unboxing logic.

Some special handling of the CLR for nullable value types

A nullable value type returns the type of its value with GetType instead of the actual type. Because actually what we're trying to get when we do this is the type of value, not the nullable<t> type. So the CLR did this processing here.

Console.WriteLine (Nullable type. GetType ()); // The return is System.Int32, not system.nullable<int32> .

Calling an interface method with a nullable value type

 Public struct where struct

The above is the definition of nullable<t>, and you can see that it does not inherit any interfaces.

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

Int32 result = ((IComparable) nullable type). CompareTo (5); // the permissible practice, which is equivalent to the following, is only to say that the CLR has done a simplified processing in this respect result = ((IComparable) (Int32) nullable type). CompareTo (5);

"C # Advanced Series" 18 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.