分类目录

链接

2012 年 1 月
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

近期文章

热门标签

新人福利,免费薅羊毛

现在位置:    首页 > .NET > 正文
C#中值为NULL时计算出错处理方法
.NET 暂无评论 阅读(2,945)
  1.  static void Main(string[] args)
  2.  {
  3.             Nullable<int> x1 = GetNullableType();//可空结构
  4.             if (x1.HasValue)//判断是否有值,也就是是否为null
  5.             {
  6.                 Console.WriteLine("x1有值得{0}", x1.Value); //获取value
  7.             }
  8.             else
  9.             {
  10.  Console.WriteLine("x1无值."); }
  11.             int? x2 = 0;
  12.             int? x3 = x1 + x2;//两个运算数据一个为null则结果为null
  13.             int y1 = x3 ?? 1;//结合运算符,如果??前的数据为null,则取null后的
  14.             Console.WriteLine(y1);
  15.             Console.ReadKey();
  16.         }
  17.         static int? GetNullableType()
  18.         {
  19.             return null;
  20.         }
  21.  
  22.  
  23.  
  24.  

============ 欢迎各位老板打赏~ ===========

本文版权归Bruce's Blog所有,转载引用请完整注明以下信息:
本文作者:Bruce
本文地址:C#中值为NULL时计算出错处理方法 | Bruce's Blog

发表评论

留言无头像?