导出 (0) 打印
全部展开
信息
您所需的主题如下所示。但此主题未包含在此库中。

Nullable<T>.GetValueOrDefault 方法 (T)

2013/12/13

检索当前 Nullable<T> 对象的值或指定的默认值。

Namespace:  System
程序集:  mscorlib(位于 mscorlib.dll 中)

public T GetValueOrDefault(
	T defaultValue
)

参数

defaultValue
类型: T
如果 HasValue 属性为 false,则为一个返回值。

返回值

类型: T
如果 HasValue 属性为 true,则为 Value 属性的值;否则为 defaultValue 参数。

即使 HasValue 属性为 falseGetValueOrDefault 方法也会返回一个值(这与 Value 属性不同,该属性将引发异常)。

下面的代码示例检索 Nullable<T> 对象的值(如果定义了该值);如果未定义该值,则检索默认值或一个特定的默认值。


// 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]

*/


Windows Phone OS

受以下版本支持: 8.0, 7.1, 7.0

Windows Phone

Microsoft 正在进行一项网上调查,以了解您对 MSDN 网站的意见。 如果您选择参加,我们将会在您离开 MSDN 网站时向您显示该网上调查。

是否要参加?
显示:
© 2014 Microsoft