int in C #? Int?? What is the meaning

Source: Internet
Author: User
what does the int in C # mean?

Int. : Represents a nullable type, which is a special value type whose value can be null
To give a variable an initial value, assign a variable (int type) to NULL instead of 0
Int.. : Used to judge and assign a value, first to determine whether the current variable is NULL, if you can corvee a new value, or skip

public int. A=null;
public int B ()
{
Return THIS.A?? 0;
}

A value type is followed by a question mark indicating nullable null (Nullable structure)

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

for a type, if you can assign a value to it, or you can assign it a null reference null (indicating that there are no values), we say that the type is nullable.

Therefore, a nullable type can represent a value, or it indicates that no value exists. For example, a reference type similar to String is a nullable type, whereas a value type similar to Int32 is not a nullable type. The Nullable structure supports extending a value type to be nullable, but not supported on reference types, because the reference type itself is nullable.

Because the capacity of a value type is only sufficient to represent a value that is appropriate for that type, it is not nullable, and the value type does not have the additional capacity required to represent a null value.

Example: public int. age;

Add: The same is true of other types of post addition questions.
int? num = null;   correct
int num=null;     error
--------------------------------- ------------------------------------------------------------------------------------------
Microsoft MSDN is prompted with the following example:
Http://msdn.microsoft.com/zh-cn/support/1t3y8s4s (vs.80). aspx

class nullableexample 
{
static void Main ()
{
int? num = null;
if (Num. HasValue = = True)
{
System.Console.WriteLine ("num =" + num.) Value);
}
Else
{
System.Console.WriteLine ("num = Null");
The

//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.mess Age);
}
}
}

The above will display output:

num = Null

Nullable object must have a value.
The
Nullable type Overview
Nullable type has the following attributes:

A nullable type represents a variable of a value type that can be assigned a null value. Cannot create a nullable type based on a reference type. (reference type has supported null values.) )。 The

Syntax T is shorthand for system.nullable<t>, where T is a value type. The two forms are interchangeable.

Assigns a value to a nullable type as the same as a method that assigns a value to a generic value type, such as int? x = 10; or double d = 4.108;.

If the value of the underlying type is NULL, use the System.Nullable.GetValueOrDefault property to return the value or default value assigned by the underlying type, such as int j = X.getvalueordefault ();

Use the HasValue and value read-only properties to test for null and retrieve values, such as if (x.hasvalue) j = x.value;

If this variable contains a value, the HasValue property returns True, or False if the value of this variable is null.

If the value is assigned, the Value property returns it, or the System.InvalidOperationException is thrown. The default value of the

Nullable type variable sets HasValue to False. Value is not defined.

The default value is assigned by using the??? operator, which is applied when a nullable type with an empty current value is assigned to a non-empty type, such as int? x = null; int y = x??-1;. The

does not allow nested nullable types to be used. The following line of:nullable<nullable<int>> N is not compiled;

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.