System.Nullable<T> Structure

public struct Nullable<T> where T : struct

Base Types

Object
  ValueType
    Nullable<T>

Assembly

mscorlib

Library

BCL

Summary

Represents a nullable value type. An instance of System.Nullable<T> can contain a value of type T or 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, T cannot be of type System.Nullable<U> for any U . end note]

Description

The Nullable<T> value type represents a value of a given type T or 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 boxed T &apos;s containing the Nullable<T>&apos;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 true or false , 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 is true ); 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 type T returns 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.

See Also

System Namespace

Members

Nullable<T> Constructors

Nullable<T> Constructor

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

Nullable<T>.HasValue Property
Nullable<T>.Value Property


Nullable<T> Constructor

public Nullable(T value)

Summary

Constructs and initializes a new instance of Nullable<T> giving it the specified initial value.

Parameters

value
The initial value of the new instance.

Description

[Note: Once this constructor has executed, applying System.Nullable<T>.HasValue to the new instance returns true .]

See Also

System.Nullable<T> Structure, System Namespace

Nullable<T>.Equals Method

public override bool Equals(object obj)

Summary

Determines whether the current instance and the specified Object represent the same type and value.

Parameters

obj
The Object to compare to the current instance.

Return Value

The following table defines the conditions under which the return value is true or false :

Returned ValueHasValue Condition
false The current instance and obj have different types.
true false
false true
true false
false false
false true
Value.Equals( obj.Value) true

Description

[Note: This method overrides System.Object.Equals(System.Object).]

See Also

System.Nullable<T> Structure, System Namespace

Nullable<T>.FromNullable Method

public static T FromNullable(Nullable<T> value)

Summary

Creates a T from a Nullable<T>.

Parameters

value
The Nullable<T> value to convert to type T .

Return Value

The value, if any, of the specified nullable value. Otherwise, a InvalidOperationException is thrown.

Exceptions

Exception TypeCondition
System.InvalidOperationExceptionSystem.Nullable<T>.HasValue is false .

Description

[Note: This method corresponds to the Op_Explicit method.]

See Also

System.Nullable<T> Structure, System Namespace

Nullable<T>.GetHashCode Method

public override int GetHashCode()

Summary

Generates a hash code for the current instance.

Return Value

If System.Nullable<T>.HasValue is true , a Int32 containing the hash code for the value of the current instance is returned; otherwise, 0 is returned.

Description

The algorithm used to generate the hash code is unspecified.

[Note: This method overrides System.Object.GetHashCode.]

See Also

System.Nullable<T> Structure, System Namespace

Nullable<T>.GetValueOrDefault() Method

public T GetValueOrDefault()

Summary

Returns the value of the current instance, or if it has none, returns the default value for the type T .

Return Value

A value of type T , which is either the value of the current instance, or if it has none, the default value for the type T (i.e., all-bits-zero).

Description

[Note: System.Nullable<T>.GetValueOrDefault(T) allows a value other than the default value to be returned if the current instance contains no value.]

See Also

System.Nullable<T> Structure, System Namespace

Nullable<T>.GetValueOrDefault(T) Method

public T GetValueOrDefault(T alternateDefaultValue)

Summary

Returns the value of the current instance, or if it has none, returns alternateDefaultValue.

Parameters

alternateDefaultValue
The value to be returned if the current instance contains no value.

Return Value

A value of type T , which is either the value of the current instance, or if it has none, the value of alternateDefaultValue.

Description

[Note: System.Nullable<T>.GetValueOrDefault() allows the default value for type T to be returned if the current instance contains no value.]

See Also

System.Nullable<T> Structure, System Namespace

Nullable<T>.ToNullable Method

public static Nullable<T> ToNullable(T value)

Summary

Creates a Nullable<T> from a T .

Parameters

value
The T value to convert to Nullable<T>.

Return Value

A Nullable<T> with the specified value.

Description

[Note: This method corresponds to the Op_Implicit method.]

See Also

System.Nullable<T> Structure, System Namespace

Nullable<T>.ToString Method

public override string ToString()

Summary

Returns a String representation of the value of the current instance.

Return Value

If System.Nullable<T>.HasValue is true, System.Nullable<T>.Value.ToString() is returned; otherwise, System.String.Empty is returned.

Description

[Note: This method overrides System.Object.ToString.]

See Also

System.Nullable<T> Structure, System Namespace

Nullable<T>.op_Explicit Method

public static explicit operator T(Nullable<T> value)

Summary

Perform an explicit conversion of a Nullable<T> value to type T .

Parameters

value
The Nullable<T> value to convert to type T .

Return Value

The value, if any, of the specified nullable value. Otherwise, a InvalidOperationException is thrown.

Exceptions

Exception TypeCondition
System.InvalidOperationExceptionSystem.Nullable<T>.HasValue is false .

Description

[Note: The conversion implemented by this method corresponds exactly to obtaining the value of the System.Nullable<T>.Value property.]

See Also

System.Nullable<T> Structure, System Namespace

Nullable<T>.op_Implicit Method

public static implicit operator Nullable<T>(T value)

Summary

Perform an implicit conversion of a T value to Nullable<T>.

Parameters

value
The T value to convert to Nullable<T>.

Return Value

A Nullable<T> with the specified value.

Description

[Note: The conversion implemented by this method corresponds exactly to invoking the System.Nullable<T>(T) constructor.]

See Also

System.Nullable<T> Structure, System Namespace

Nullable<T>.HasValue Property

public bool HasValue { get; }

Summary

Indicates whether the current instance contains a value.

Property Value

true if the current instance contains a value; otherwise false .

Description

[Behaviors: If System.Nullable<T>.HasValue is true , 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.

]

See Also

System.Nullable<T> Structure, System Namespace

Nullable<T>.Value Property

public T Value { get; }

Summary

Gets the value, if any, of the current instance.

Property Value

The value of the current instance.

Exceptions

Exception TypeCondition
System.InvalidOperationExceptionSystem.Nullable<T>.HasValue is false .

Description

[Behaviors: If System.Nullable<T>.HasValue is true , 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.

]

See Also

System.Nullable<T> Structure, System Namespace