首页 > 其他 > 详细

可空类型

时间:2014-07-21 10:03:16      阅读:386      评论:0      收藏:0      [点我收藏+]

可空类型

  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.   

bubuko.com,布布扣
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

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

  bubuko.com,布布扣

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

可空类型,布布扣,bubuko.com

可空类型

原文:http://www.cnblogs.com/tekkaman/p/3856099.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!