Int in C?

Source: Internet
Author: User

Int? : Indicates an empty type, which is a special value type. Its value can be null.
It is used to assign null to the variable (INT type) when setting the initial value, instead of 0.
Int ?? : Used to determine and assign values. First, judge whether the current variable is null. If yes, you can activate a new value. Otherwise, Skip.

 
1 Public Int? A =Null;
2 Public IntB ()
3{
4 Return This. ??0;
5}

The value type is followed by a question mark to indicate null (nullable structure)

Nullable is a new technique provided in. NET 2.0 to indicate whether a value type can be empty.

For a type, if you can either assign a value to it or assign a null reference to it (indicating that there is no value), we can say that this type can be empty.

Therefore, an empty type can indicate a value, or a value does not exist. For example, the reference type similar to string is the void type, while the value type similar to int32 is not the void type. The nullable structure supports extending the value type to null, but does not support the use of the reference type, because the reference type itself is empty.

Because the capacity of the value type is sufficient to indicate a value suitable for this type, it cannot be blank; the value type does not indicate the additional capacity required for the null value.

Example:

 
Public Int? Age;

Supplement: The same applies to other types of post-addition problems.

 
Int? Num =Null;//Correct
IntNum =Null;//Error

The following is an example of Microsoft msdn prompt:

 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.

Empty type Overview
The null type has the following features:

The 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? YesSystem. nullable <t>Here, 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, useSystem. nullable. getvalueordefaultProperty returns 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 it is null or retrieve the value. 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, as shown in figureInt? X = NULL;IntY = x ?? -1 ;.

Nested empty types are not allowed. The following line is not compiled: nullable <int> N;

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.