public struct Nullable<T> where T : struct
Object
ValueType
Nullable<T>
mscorlib
BCL
Represents a nullable value type. An instance of System.Nullable<T> can contain a value of typeTor an indication that the instance contains no value. Upon boxing, if it contains no value, it will be converted to the null reference; otherwise, it will be converted to a boxed T. [Note: Because of the constraint on the generic parameter,Tcannot be of type System.Nullable<U> for anyU. end note]
The Nullable<T> value type represents a value of a given typeTor an indication that the instance contains no value. Such a nullable type is useful in a variety of situations, such as in denoting nullable columns in a database table or optional attributes in an XML element. The runtime transforms Nullable<T>instances without values into true nulls when performing a box operation; instances with values are transformed into boxedT's containing the Nullable<T>'s Value.An instance of Nullable<T> has two properties, System.Nullable<T>.HasValue and System.Nullable<T>.Value. System.Nullable<T>.HasValue is used to determine whether the current instance currently has a value. It returns
trueorfalse, and never throws an exception. System.Nullable<T>.Value returns the current value of the instance, provided it has one (i.e., System.Nullable<T>.HasValue istrue); otherwise, it throws an exception.In addition to the above properties, there is a pair of methods, both overloads of System.Nullable<T>.GetValueOrDefault. The version taking no arguments returns the instance's current value, if it has one; otherwise, it returns the default value of type
T. The version taking an argument of typeTreturns the instance's current value, if it has one; otherwise, it returns the default value argument passed to it.Applying System.Nullable<T>.HasValue to an instance that has the default initial value, causes false to be returned.
System Namespace
Nullable<T> Constructors
Nullable<T> Methods
Nullable<T>.Equals Method
Nullable<T>.FromNullable Method
Nullable<T>.GetHashCode Method
Nullable<T>.GetValueOrDefault() Method
Nullable<T>.GetValueOrDefault(T) Method
Nullable<T>.ToNullable Method
Nullable<T>.ToString Method
Nullable<T>.op_Explicit Method
Nullable<T>.op_Implicit Method
Nullable<T> Properties
public Nullable(T value)
Constructs and initializes a new instance of Nullable<T> giving it the specified initial value.
- value
- The initial value of the new instance.
[Note: Once this constructor has executed, applying System.Nullable<T>.HasValue to the new instance returnstrue.]
System.Nullable<T> Structure, System Namespace
public override bool Equals(object obj)
Determines whether the current instance and the specified Object represent the same type and value.
- obj
- The Object to compare to the current instance.
The following table defines the conditions under which the return value istrueorfalse:
Returned Value HasValue Condition falseThe current instance and obj have different types. truefalsefalsetruetruefalsefalsefalsefalsetrueValue.Equals(obj.Value)true
[Note: This method overrides System.Object.Equals(System.Object).]
System.Nullable<T> Structure, System Namespace
public static T FromNullable(Nullable<T> value)
Creates aTfrom a Nullable<T>.
- value
- The Nullable<T> value to convert to type
T.
The value, if any, of the specified nullable value. Otherwise, a InvalidOperationException is thrown.
Exception Type Condition System.InvalidOperationException System.Nullable<T>.HasValue is false.
[Note: This method corresponds to theOp_Explicitmethod.]
System.Nullable<T> Structure, System Namespace
public override int GetHashCode()
Generates a hash code for the current instance.
If System.Nullable<T>.HasValue istrue, a Int32 containing the hash code for the value of the current instance is returned; otherwise, 0 is returned.
The algorithm used to generate the hash code is unspecified.[Note: This method overrides System.Object.GetHashCode.]
System.Nullable<T> Structure, System Namespace
public T GetValueOrDefault()
Returns the value of the current instance, or if it has none, returns the default value for the typeT.
A value of typeT, which is either the value of the current instance, or if it has none, the default value for the typeT(i.e., all-bits-zero).
[Note: System.Nullable<T>.GetValueOrDefault(T)allows a value other than the default value to be returned if the current instance contains no value.]
System.Nullable<T> Structure, System Namespace
public T GetValueOrDefault(T alternateDefaultValue)
Returns the value of the current instance, or if it has none, returns alternateDefaultValue.
- alternateDefaultValue
- The value to be returned if the current instance contains no value.
A value of typeT, which is either the value of the current instance, or if it has none, the value of alternateDefaultValue.
[Note: System.Nullable<T>.GetValueOrDefault()allows the default value for typeTto be returned if the current instance contains no value.]
System.Nullable<T> Structure, System Namespace
public static Nullable<T> ToNullable(T value)
Creates a Nullable<T> from aT.
- value
- The
Tvalue to convert to Nullable<T>.
A Nullable<T> with the specified value.
[Note: This method corresponds to theOp_Implicitmethod.]
System.Nullable<T> Structure, System Namespace
public override string ToString()
Returns a String representation of the value of the current instance.
If System.Nullable<T>.HasValue is true, System.Nullable<T>.Value.ToString()is returned; otherwise, System.String.Empty is returned.
[Note: This method overrides System.Object.ToString.]
System.Nullable<T> Structure, System Namespace
public static explicit operator T(Nullable<T> value)
Perform an explicit conversion of a Nullable<T> value to typeT.
- value
- The Nullable<T> value to convert to type
T.
The value, if any, of the specified nullable value. Otherwise, a InvalidOperationException is thrown.
Exception Type Condition System.InvalidOperationException System.Nullable<T>.HasValue is false.
[Note: The conversion implemented by this method corresponds exactly to obtaining the value of the System.Nullable<T>.Value property.]
System.Nullable<T> Structure, System Namespace
public static implicit operator Nullable<T>(T value)
Perform an implicit conversion of aTvalue to Nullable<T>.
- value
- The
Tvalue to convert to Nullable<T>.
A Nullable<T> with the specified value.
[Note: The conversion implemented by this method corresponds exactly to invoking the System.Nullable<T>(T)constructor.]
System.Nullable<T> Structure, System Namespace
public bool HasValue { get; }
Indicates whether the current instance contains a value.
trueif the current instance contains a value; otherwisefalse.
[Behaviors: If System.Nullable<T>.HasValue istrue, the instance contains a value, and System.Nullable<T>.Value returns that value.If System.Nullable<T>.HasValue is
false, the instance contains no value, and an attempt to read System.Nullable<T>.Value results in a System.InvalidOperationException exception. A call to System.Nullable<T>.Value.GetValueOrDefault returns the default value.This property is read-only.
]
System.Nullable<T> Structure, System Namespace
public T Value { get; }
Gets the value, if any, of the current instance.
The value of the current instance.
Exception Type Condition System.InvalidOperationException System.Nullable<T>.HasValue is false.
[Behaviors: If System.Nullable<T>.HasValue istrue, the instance contains a value, and System.Nullable<T>.Value returns that value.If System.Nullable<T>.HasValue is
false, the instance contains no value, and an attempt to read System.Nullable<T>.Value results in an exception.This property is read-only.
]
System.Nullable<T> Structure, System Namespace