Tekkaman

导航

 

可空类型

  Nullable types are instances of the System.Nullable<T> struct.  Nullable<bool> can be assigned the values true false, or nullThe ability to assign null to numeric and Boolean types is especially useful when you are dealing with databases and other data types that contain elements that may not be assigned a value.   

class NullableExample
{
    static void Main()
    {
        int? num = null;

        // Is the HasValue property true?
        if (num.HasValue)
        {
            System.Console.WriteLine("num = " + num.Value);
        }
        else
        {
            System.Console.WriteLine("num = Null");
        }

        // y is set to zero
        int y = num.GetValueOrDefault();

        // num.Value throws an InvalidOperationException if num.HasValue is false
        try
        {
            y = num.Value;
        }
        catch (System.InvalidOperationException e)
        {
            System.Console.WriteLine(e.Message);
        }
    }
}
View Code

  ??可配置可空类型使用:

  

参考:http://msdn.microsoft.com/zh-cn/library/1t3y8s4s.aspx

posted on 2014-07-20 12:45  Tekkaman  阅读(199)  评论(0编辑  收藏  举报