C # null Type Analysis

Source: Internet
Author: User

For example, if nullable <int32> is read as an "Optional int32", it can be assigned any value between-2147483648 and 2147483647, or it can be assigned a null value. Nullable <bool> can be assigned true, false, or null. It is particularly useful to assign null values to numeric or boolean values when processing databases and other data types that contain elements that may not be assigned a value. For example, a Boolean field in a database can store values of true or false, or the field can be undefined.
The null type has the following features:
· A value type variable that can be assigned a null value. An empty type based on the reference type cannot be created. (The reference type supports null values .).
· Syntax t? Is short for system. nullable <t>, where T is the value type. These two forms can be exchanged.
· The method for assigning null values is the same as that for assigning values to common values, such as Int? X = 10; or double? D = 4.108 ;.
· If the base type value is null, use the system. nullable. getvalueordefault attribute to return the value or default value of the base type, for example, Int J = x. getvalueordefault ();
· Use the read-only attributes of hasvalue and value to test whether the value is null or not, for example, if (X. hasvalue) J = x. value;
If this variable contains a value, the hasvalue attribute returns true; or if the value of this variable is null, false is returned.
If a value is assigned, the Value Attribute returns this value. Otherwise, system. invalidoperationexception is thrown.
Set hasvalue to false by default for empty type variables. Undefined value.
· Use ?? The default value is assigned to operators. If the null type is assigned to a non-null type, the default value is applied, such as Int? X = NULL; int y = x ?? -1 ;.
· Nested empty types are not allowed. The following line is not compiled: nullable <int> N;
Program Code Program code
Class nullableexample
{
Static void main ()
{
Int? Num = NULL;
If (Num. hasvalue = true)
{
System. Console. writeline ("num =" + num. value );
}
Else
{
System. Console. writeline ("num = NULL ");
}
// Y is set to zero
Int y = num. getvalueordefault ();
// Num. Value throws an invalidoperationexception if num. hasvalue is false
Try
{
Y = num. value;
}
Catch (system. invalidoperationexception E)
{
System. Console. writeline (E. Message );
}
}
}
The output is as follows:
Num = NULL
Nullable object must have a value.

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.