157 recommendations for writing high-quality code to improve C # programs--Recommendation 5: Use int to ensure that value types can also be null

Source: Internet
Author: User

Recommendation 5: Use int to ensure that value types can also be null

Why does the primitive type need to be null? Consider two scenarios:

1) An int field in the database can be set to null. In C #, after the value is taken out, in order to assign it to the int type, you have to first determine if it is null. If NULL is directly assigned to the int type, an exception is thrown.

2) in a distributed system, the server needs to receive and parse the data from the client. An int data may be lost or tampered with during transmission, and should be saved as a null value after a transformation fails, rather than providing an initial value.

There are a lot of similar scenarios, so starting with. NET 2.0, an additional type is available in the FCL: Nullable type nullable<t>. It is a struct, declared as follows:

    [SerializableAttribute]      publicstructwherestruct

Because it is a struct, only the value reference type can be a nullable type (the reference type itself can be null). A nullable int type is represented as:

Nullable<int> i = null;

It can also be expressed as:

Int? i = null;

Grammar T is a shorthand for nullable<t>, and they can be converted to each other. A nullable type represents a value that is within the normal range of its underlying value type plus a null value. For example, Nullable<int32> The range of values is-2 147 483 648 ~ 2 147 483 647, plus a null value.

Now let's look at the reciprocal conversions of nullable types and primitive types. The primitive type provides an implicit conversion of its corresponding nullable type, as follows:

    int NULL ;       int 0 ;      

In turn, nullable types are not implicitly convertible to the corresponding primitive type, and the correct conversion form is as follows:

    int 123 ;       int J;       if (I.hasvalue)      {          = i.value;      }       Else       {          0;      

But does this code look a bit cumbersome? So, when explaining the nullable type, you have to mention?? Operator.?? The best thing to do is to assign values of nullable types to the corresponding primitive types to simplify, a simplified form of the preceding code is:

    int 123 ;       int 0

Int J = I?? 0; means that if the hasvalue of I is true, the value of I is assigned to J; otherwise, the value of J is assigned to 0.

Turn from: 157 recommendations for writing high-quality code to improve C # programs Minjia

157 recommendations for writing high-quality code to improve C # programs--Recommendation 5: Use int to ensure that value types can also be null

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.