当前位置: 首页>>代码示例>>C#>>正文


C# Nullable<T>.GetValueOrDefault方法代码示例

本文整理汇总了C#中System.Nullable<T>.GetValueOrDefault方法的典型用法代码示例。如果您正苦于以下问题:C# Nullable<T>.GetValueOrDefault方法的具体用法?C# Nullable<T>.GetValueOrDefault怎么用?C# Nullable<T>.GetValueOrDefault使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Nullable<T>的用法示例。


在下文中一共展示了Nullable<T>.GetValueOrDefault方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

// This code example demonstrates the 
// Nullable<T>.GetValueOrDefault methods.

using System;

class Sample 
{
    public static void Main() 
    {
    float? mySingle = 12.34f;
    float? yourSingle = -1.0f;

   Console.WriteLine("*** Display a value or the default value ***\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;

    Console.Write("\n*** Display a value or the ");
    Console.WriteLine("specified default value ***\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);    
    }
}
开发者ID:.NET开发者,项目名称:System,代码行数:71,代码来源:Nullable.GetValueOrDefault

输出:

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]


注:本文中的System.Nullable<T>.GetValueOrDefault方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。