Go to: details about the null type nullable

Source: Internet
Author: User

The null type is the real column of the system. nullable struct. An empty type indicates that a null value is appended to the correct range of the corresponding value type. In this case, it is actually not very clear. The proposition is generally not easy to understand, but accurate.

Let me talk about this empty type. The value type and reference type are mentioned last time, and the value type cannot be blank, int I = NULL is wrong. The value type can be null, but sometimes we need to leave the value type empty. What should we do, then, expand a null value in the range of value types.

    1. Why is there an empty type?
    2.  

In daily development, where do we need to change the value type to be empty? If you have tried to visualize the data in the database, the fields in the database are defined as follows:

Convert to object

 
Public class XXXX {public int ID {Get; set ;}//......}

 

At this time, the problem occurs. It is allowed to be null in the database. However, in the class design, the int type is not allowed to be null. You do not assign a value to it, it also has a default value of 0. What should I do? This is in conflict with the database design. Therefore, we must use our empty type. I personally understand that this is the reason for the possible empty type.

How does one indicate a null type?

The class can be written

 
Public class XXXX {public Int? Id {Get; set ;}//......}

 

It is correct. Is it added after Int? This is the representation of the null type. Of course there are other representation methods.

Nullable <int> d = NULL;

These two representations are actually equivalent, so you can use them flexibly based on your preferences and scenarios.

How can I determine whether it is null or another value?

For the null type, we can easily compare null with the null type to determine whether the variable value is null. For example:

 
Nullable <int> d = NULL; bool isnull = d = NULL? True: false;

Of course, I have other ways to do things that have the same effect as the method, such:

 
Nullable <int> d = NULL; bool isnull =! D. hasvalue;

Note:If the value of hasvalue is false and VAR result = D. value is used, system. invalidoperationexception is thrown.

The above two judgment methods can be used flexibly as needed.

 

How can I assign a value to a null type?

The null type cannot be directly converted to the value type, or you cannot directly assign values to the value type, for example:

 
Nullable <int> d = NULL; int result = D; // error int result = (INT) D; // Error

So how to do it? The simplest way is

Nullable <int> d = NULL; int result = D. getvalueordefault ();

 

The result value is 0;

Or you can determine whether it is null and assign a value to it.

 
Nullable <int> d = NULL; int result; If (! D. hasvalue) {result = D. value ;}

 

 

If the value type is null, a value type is assigned. There is another way.

 
Nullable <int> d = NULL; int result = D ?? + 1;

 

Use ?? . The result value is 1;

Empty type in reflection

We often use the void type in reflection. How can we use the void type?

 
Public class nullabletest {public Int? Id {Get; Set ;}}

 

 
VaR propertyinfo = typeof (nullabletest). getproperty ("ID ");

 

When querying the value of a variable, we will find that

Propertyinfo. propertytype. Name Nullable '1
Propertyinfo. propertytype. isgenerictype True
Propertyinfo. propertytype. isgenerictypedefinition False
Propertyinfo. propertytype. getgenerictypedefinition (). Name Nullable '1

We cannot use the getgenerictypedefinition () method to obtain the generic base type at all. In this case, we need to use getgenericarguments () to obtain the basic generic type.

VaR propertyinfo = typeof (nullabletest ). getproperty ("ID"); If (propertyinfo. propertytype. isgenerictype & propertyinfo. propertytype. getgenerictypedefinition () = typeof (nullable <>) {type [] typearray = propertyinfo. propertytype. getgenericarguments (); Type basetype = typearray [0];}

 

Here, we can use reflection to complete any operations we want.

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.