Int? : Represents a nullable type, which is a special value type whose value can be null
For assigning variables (int type) to NULL, instead of 0, when setting the initial value of the variable
Int?? : Used to judge and assign a value, first determine whether the current variable is null, if it is possible to corvee a new value, otherwise skip
public int? A=null;
public int B ()
{
Return THIS.A?? 0;
}
A question mark followed by a value type indicates nullable null (Nullable structure)
Nullable is a technique that is newly 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, you can assign it a null reference, NULL (indicating that there is no value), and 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 String-like reference type is a nullable type, and 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 a value type has only enough capacity to represent a value that is appropriate for that type, it cannot be empty, and the value type has no additional capacity required to represent a null value.
Example: public int? Age
Add: Other types of post-add problems are the same.
Int? num = null; That's right
int num=null; Error
--------------------------------------------------------------------------------------------------------------- ------------
Microsoft MSDN is a hint of 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");
}
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 above will show the output:
num = Null
Nullable object must has a value.
Overview of nullable types
Nullable types have the following characteristics:
A nullable type represents a value type variable that can be assigned a value of NULL. You cannot create a nullable type based on a reference type. (The reference type already supports null values.) )。
Grammar T? is a shorthand for system.nullable<t>, where T is a value type. These two forms are interchangeable.
Assigning a value to a nullable type is the same as assigning a value to a generic value type, such as an 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 to the underlying type, such as int j = X.getvalueordefault ();
Use the HasValue and value read-only properties to test if 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 this, or System.InvalidOperationException is thrown.
The default value of the nullable type variable sets HasValue to False. Value is not defined.
Use?? operator assigns a default value, and the nullable type with the current value of NULL is assigned to a non-null type when the default value is applied, such as int? x = null; int y = x?? -1;.
The use of nested nullable types is not allowed. The following line will not be compiled:nullable<nullable<int>> N;
Reproduced in original: http://www.cnblogs.com/firstcsharp/archive/2011/12/11/2283797.html
what does int in C # mean?