[Reading Notes] C # Empty data type

Source: Internet
Author: User

Numbers andProgramming LanguageNumbers in the database can be empty, and numbers in C # cannot be empty. Int32 is a structure, while the structure is of the value type. Therefore, it cannot be empty.

 

In C #, you can use the nullable <t> structure to easily solve this problem.

Define an empty type:

 
Nullable <int> X; Int? X2;

 

Check the hasvalue and value Attributes of nullable <t>.

 
Nullable <int> X; X = 4; x + = 3; if (X. hasvalue) {int y = x. value;} X = NULL;

 

NULL data types can also use Arithmetic Operators. The X3 variable is the sum of the X1 and X2 variables. If one of the two empty variables is null, their sum is null.

Int? X1 = getnullabletype ();

Int? X2 = getnullabletype ();

Int? X3 = X1 + x2;

 

Convert the data type that can be empty to non-empty:

First, display Conversion

Int? X1 = getnullabletype ();

Int Y1 = (INT) x1

Second, it is safer to use the concatenation operator to convert

Int? X1 = getnullabletype ();

Int Y1 = x1 ?? 0;

 

When you use the getnullabletype () method, you will find such an error message.

The name 'getnulllabletype' does not exist in the current context

This should be because the namespace of the getnullabletype method is not added, but the namespace in which this method does not find o (operator □operator) O

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.