Use a value type that can be Null in. NET

Source: Internet
Author: User

In.. NET. The value type is stored in the memory stack and cannot be null. The reference type is stored in the Heap of the memory, it can be Null. The value types include common int, decimal, Datetime, etc. The general value types are inherited from struct, and the reference types are similar to string, ArrayList, and so on. What should we do if we want to set the value type to null.

In practical use, there are also cases where the value type is set to null, such as DateTime type. If it is obtained from the database, the database is null, how should I set it in my program? If it is set to the default initialization value, it certainly does not meet the requirements, so we 'd better set it to Null. How should we set it.

In C #, you can wrap the value type into a reference type using a very simple statement, that is, using question marks, as shown below:

Int? I = null;

At this time, we can find that the int type can also be set to null.

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.