共用方式為


Nullable<T>.GetValueOrDefault Method (T)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Retrieves the value of the current Nullable<T> object, or the specified default value.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Function GetValueOrDefault ( _
    defaultValue As T _
) As T
public T GetValueOrDefault(
    T defaultValue
)

Parameters

  • defaultValue
    Type: T
    A value to return if the HasValue property is false.

Return Value

Type: T
The value of the Value property if the HasValue property is true; otherwise, the defaultValue parameter.

Remarks

The GetValueOrDefault method returns a value even if the HasValue property is false (unlike the Value property, which throws an exception).

Examples

The following code example retrieves the value of a Nullable<T> object if that value is defined; otherwise, it retrieves the default value or a specific default value.

' This code example demonstrates the 
' Nullable(Of T).GetValueOrDefault methods.


Class Example
    Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
        Dim mySingle As Nullable(Of System.Single) = 12.34F
        Dim yourSingle As Nullable(Of System.Single) = -1.0F

        outputBlock.Text &= "*** Display a value or the default value ***" & vbCrLf
        ' Display the values of mySingle and yourSingle.

        Display(outputBlock, "A1", mySingle, yourSingle)

        outputBlock.Text &= vbCrLf

        yourSingle = mySingle.GetValueOrDefault()
        Display(outputBlock, "A2", mySingle, yourSingle)

        outputBlock.Text &= vbCrLf

        ' Assign null (Nothing in Visual Basic) to mySingle, which means no value is
        ' defined for mySingle. Then assign the value of mySingle to yourSingle and
        ' display the values of both variables. The default value of all binary zeroes 
        ' is assigned to yourSingle because mySingle has no value.
        mySingle = Nothing
        yourSingle = mySingle.GetValueOrDefault()
        Display(outputBlock, "A3", mySingle, yourSingle)
        outputBlock.Text &= vbCrLf

        ' Reassign the original values of mySingle and yourSingle.
        mySingle = 12.34F
        yourSingle = -1.0F

        outputBlock.Text &= vbCrLf & "*** Display a value or the "
        outputBlock.Text &= "specified default value ***" & vbCrLf & vbCrLf

        ' Display the values of mySingle and yourSingle.
        Display(outputBlock, "B1", mySingle, yourSingle)
        outputBlock.Text &= vbCrLf

        ' Assign the value of mySingle to yourSingle, then display the values 
        ' of mySingle and yourSingle. The yourSingle variable is assigned the 
        ' value 12.34 because mySingle has a value.
        yourSingle = mySingle.GetValueOrDefault(-222.22F)
        Display(outputBlock, "B2", mySingle, yourSingle)
        outputBlock.Text &= vbCrLf

        ' Assign null (Nothing in Visual Basic) to mySingle, which means no value is
        ' defined for mySingle. Then assign the value of mySingle to yourSingle and
        ' display the values of both variables. The specified default value of -333.33
        ' is assigned to yourSingle because mySingle has no value.
        mySingle = Nothing
        yourSingle = mySingle.GetValueOrDefault(-333.33F)
        Display(outputBlock, "B3", mySingle, yourSingle)

    End Sub


    ' Display the values of two nullable of System.Single structures.
    ' The Console.WriteLine method automatically calls the ToString methods of 
    ' each input argument to display its values. If no value is defined for a
    ' nullable type, the ToString method for that argument returns the empty
    ' string ("").

    Public Shared Sub Display(ByVal outputBlock As System.Windows.Controls.TextBlock, _
                              ByVal title As String, _
                              ByVal dspMySingle As Nullable(Of System.Single), _
                              ByVal dspYourSingle As Nullable(Of System.Single))
        If (True) Then
            outputBlock.Text += String.Format("{0}) & mySingle = [{1}], yourSingle = [{2}]", _
                               title, dspMySingle, dspYourSingle)
        End If
    End Sub
End Class

'
'This code example produces the following results:
'
'A1) mySingle = [12.34], yourSingle = [-1]
'A2) mySingle = [12.34], yourSingle = [12.34]
'A3) mySingle = [], yourSingle = [0]
'
'*** Display a value or the specified default value ***
'
'B1) mySingle = [12.34], yourSingle = [-1]
'B2) mySingle = [12.34], yourSingle = [12.34]
'B3) mySingle = [], yourSingle = [-333.33]
'
// This code example demonstrates the 
// Nullable<T>.GetValueOrDefault methods.

using System;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      float? mySingle = 12.34f;
      float? yourSingle = -1.0f;

      outputBlock.Text += "*** Display a value or the default value ***\n" + "\n";
      // Display the values of mySingle and yourSingle.

      Display("A1", mySingle, yourSingle);

      // Assign the value of mySingle to yourSingle, then display the values 
      // of mySingle and yourSingle. The yourSingle variable is assigned the 
      // value 12.34 because mySingle has a value.

      yourSingle = mySingle.GetValueOrDefault();
      Display("A2", mySingle, yourSingle);

      // Assign null (Nothing in Visual Basic) to mySingle, which means no value is
      // defined for mySingle. Then assign the value of mySingle to yourSingle and
      // display the values of both variables. The default value of all binary zeroes 
      // is assigned to yourSingle because mySingle has no value.

      mySingle = null;
      yourSingle = mySingle.GetValueOrDefault();
      Display("A3", mySingle, yourSingle);

      // Reassign the original values of mySingle and yourSingle.
      mySingle = 12.34f;
      yourSingle = -1.0f;

      outputBlock.Text += "\n*** Display a value or the ";
      outputBlock.Text += "specified default value ***\n" + "\n";

      // Display the values of mySingle and yourSingle.
      Display("B1", mySingle, yourSingle);

      // Assign the value of mySingle to yourSingle, then display the values 
      // of mySingle and yourSingle. The yourSingle variable is assigned the 
      // value 12.34 because mySingle has a value.

      yourSingle = mySingle.GetValueOrDefault(-222.22f);
      Display("B2", mySingle, yourSingle);

      // Assign null (Nothing in Visual Basic) to mySingle, which means no value is
      // defined for mySingle. Then assign the value of mySingle to yourSingle and
      // display the values of both variables. The specified default value of -333.33
      // is assigned to yourSingle because mySingle has no value.

      mySingle = null;
      yourSingle = mySingle.GetValueOrDefault(-333.33f);
      Display("B3", mySingle, yourSingle);

   }

   // Display the values of two nullable of System.Single structures.
   // The Console.WriteLine method automatically calls the ToString methods of 
   // each input argument to display its values. If no value is defined for a
   // nullable type, the ToString method for that argument returns the empty
   // string ("").
   public static void Display(string title, float? dspMySingle, float? dspYourSingle)
   {
      Console.WriteLine("{0}) mySingle = [{1}], yourSingle = [{2}]",
                        title, dspMySingle, dspYourSingle);
   }
}

/*
This code example produces the following results:

A1) mySingle = [12.34], yourSingle = [-1]
A2) mySingle = [12.34], yourSingle = [12.34]
A3) mySingle = [], yourSingle = [0]

_** Display a value or the specified default value **_

B1) mySingle = [12.34], yourSingle = [-1]
B2) mySingle = [12.34], yourSingle = [12.34]
B3) mySingle = [], yourSingle = [-333.33]

*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.