分享

C#中的可空类型运算(Nullable<T>)------T?

 贾朋亮博客 2014-05-21

几天在看公司的代码的时候,发现对于一些字符串的运算没有判断是否为null,就直接参与了运算,于是就引起了我的兴趣,自己写了个代码作为测试,代码如下:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace Null值相加  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             //两个null字符串相加  
  13.             string str1 = null;  
  14.             string str2 = null;  
  15.             string str3 = str1 + str2;  
  16.             Console.WriteLine("==>两个为null的字符串相加的结果:\n==>" +  
  17.                 (str3 == null  "null" : (str3.ToString() == string.Empty ? "string.Empty --> 居然为长度为0的字符串。" : str3.ToString())));  
  18.             Console.WriteLine("==>相加后字符串[str3]的长度为:" + str3.Length + System.Environment.NewLine);  
  19.             Console.WriteLine("==>我操,这是什么原因??????\n==>按任意键继续......\n\n=====================\n");  
  20.   
  21.             //一个null字符串和一个非null字符串相加  
  22.             string str11 = null;  
  23.             string str22 = "test";  
  24.             string str33 = str11 + str22;  
  25.             Console.WriteLine("==>一个null字符串和一个非null字符串的结果:\n==>" +  
  26.                               (str3 == null  "null" : (str33.ToString() == string.Empty ?  
  27.                                 "string.Empty --> 居然为长度为0的字符串。" : str33.ToString())));  
  28.             Console.WriteLine("==>相加后字符串[str33]的长度为:" + str33.Length + System.Environment.NewLine);  
  29.             Console.WriteLine(@"==>我操,对于引用类型来说,一个位null的引用类型和一个不为null的引用类型相加,结果为不为null。\n  
  30.                                 ==>按任意键继续......\n\n=====================\n");  
  31.   
  32.             //两个为null的可空整形相加  
  33.             int a = null;  
  34.             int b = null;  
  35.             int c = a + b;  
  36.             Console.WriteLine("==>两个为[null]的可空[int?]类型相加的结果:\n" +   
  37.                                 (c == null  "==>结果为null" : "没猜出来是什么结果") + System.Environment.NewLine);  
  38.             Console.WriteLine("==>我操!!!!!!!!!!!\n==>按任意键继续......\n\n");  
  39.   
  40.             //一个为null可空的[int?]类型与一个[int]相加  
  41.             int na = null;  
  42.             int nb = 11;  
  43.             int nc = na + nb;  
  44.             Console.WriteLine("==>一个为null的可空[int?]类型与一个[int]相加的结果:\n"   
  45.                                 + (nc == null  "==>结果为null" : nc.Value.ToString()) + System.Environment.NewLine);  
  46.             Console.WriteLine("==>我操,值为null的可控类型与不为null的可控类型相加其值为null.\n==>按任意键继续......\n\n");  
  47.   
  48.             Console.ReadKey();  
  49.         }  
  50.     }  
  51. }  


结果截图:

 

后来查了MSDN的相关说明,我把MSDN的说明贴上来:

 

可以为 null 的类型可以表示基础类型的所有值,另外还可以表示 null 值。可以为 null 的类型可通过下面两种方式中的一种声明:

System.Nullable<T> variable

- 或 -

T? variable

T 是可以为 null 的类型的基础类型。T 可以是包括struct 在内的任何值类型;但不能是引用类型。

有关可能使用可以为 null 的类型的示例,请考虑普通的布尔变量如何能够具有两个值:true 和 false。不存在表示“未定义”的值。在很多编程应用中(最突出的是数据库交互),变量可以以未定义的状态出现。例如,数据库中的某个字段可能包含值 true 或 false,但是它也可能根本不包含值。同样,可以将引用类型设置为 null,以指示它们未初始化。

这种不一致会导致额外的编程工作,如使用附加变量来存储状态信息、使用特殊值,等等。可以为 null 的类型修饰符使 C# 能够创建表示未定义值的值类型变量。

 

可以为 null 的类型的每个实例都具有两个公共的只读属性:

  • HasValue

    HasValue 属于 bool 类型。当变量包含非 null 值时,它被设置为true

  • Value

    Value 的类型与基础类型相同。如果HasValuetrue,则说明Value 包含有意义的值。如果HasValuefalse,则访问Value 将引发InvalidOperationException

  1. int x = 10;  
  2. if (x.HasValue)  
  3. {  
  4.     System.Console.WriteLine(x.Value);  
  5. }  
  6. else  
  7. {  
  8.     System.Console.WriteLine("Undefined");  
  9. }  


 测试代码:

  1. int y = 10;  
  2. if (y != null)  
  3. {  
  4.     System.Console.WriteLine(y.Value);  
  5. }  
  6. else  
  7. {  
  8.     System.Console.WriteLine("Undefined");  
  9. }  


可以为 null 的类型可强制转换为常规类型,方法是使用强制转换来显式转换或者通过使用 Value 属性来转换。例如:

 

  1. int n = null;  
  2.   
  3. //int m1 = n;      // 隐式转换不行  
  1. int m2 = (int)n;   // Compiles, but will create an exception if n is null.会触发异常  
  2. int m3 = n.Value;  // Compiles, but will create an exception if n is null.会触发异常  

可使用 null 关键字将可以为 null 的类型的变量设置为 null,如以下示例所示:

 

  1. int n1 = null;  


从普通类型到可以为 null 的类型的转换是隐式的。

  1. int n2;  
  2. n2 = 10;  // Implicit conversion.  


可以为 null 的类型还可以使用预定义的一元和二元运算符,以及现有的任何用户定义的值类型运算符。如果操作数为 null,这些运算符将产生一个 null 值;否则运算符将使用包含的值来计算结果。例如:

  1. int a = 10;  
  2. int b = null;  
  3.   
  4. a++;         // Increment by 1, now a is 11.  
  5. a = a * 10;  // Multiply by 10, now a is 110.  
  6. a = a + b;   // Add b, now a is null.此时以为一个运算为null所以结果为null  

在对可以为 null 的类型执行比较时,如果其中一个可以为 null 的类型的值为 null,但另外一个类型的值不为 null,则除!=(不等于)外,所有比较的结果都将为false一定不要以为由于一个特定比较的结果为false,相反的情况就会为true在以下示例中,10 不大于、小于或等于 null。只有num1 != num2 的计算结果为true

 

  1. int num1 = 10;  
  2. int num2 = null;  
  3. if (num1 >= num2)  
  4. {  
  5.     Console.WriteLine("num1 is greater than or equal to num2");  
  6. }  
  7. else  
  8. {  
  9.     // This clause is selected, but num1 is not less than num2.  
  10.     Console.WriteLine("num1 >= num2 returned false (but num1 < num2 also is false)");  
  11. }  
  12.   
  13. if (num1 < num2)  
  14. {  
  15.     Console.WriteLine("num1 is less than num2");  
  16. }  
  17. else  
  18. {  
  19.     // The else clause is selected again, but num1 is not greater than  
  20.     // or equal to num2.  
  21.     Console.WriteLine("num1 < num2 returned false (but num1 >= num2 also is false)");  
  22. }  
  23.   
  24. if (num1 != num2)  
  25. {  
  26.     // This comparison is true, num1 and num2 are not equal.  
  27.     Console.WriteLine("Finally, num1 != num2 returns true!");  
  28. }  
  29.   
  30. // Change the value of num1, so that both num1 and num2 are null.  
  31. num1 = null;  
  32. if (num1 == num2)  
  33. {  
  34.     // The equality comparison returns true when both operands are null.  
  35.     Console.WriteLine("num1 == num2 returns true when the value of each is null");  
  36. }  
  37.   
  38. /* Output: 
  39.  * num1 >= num2 returned false (but num1 < num2 also is false) 
  40.  * num1 < num2 returned false (but num1 >= num2 also is false) 
  41.  * Finally, num1 != num2 returns true! 
  42.  * num1 == num2 returns true when the value of each is null 
  43.  */  


运算符定义在将可以为 null 的类型分配给非可以为 null 的类型时返回的默认值。

 

  1. int c = null;  
  2.   
  3. // d = c, unless c is null, in which case d = -1.  
  4. int d = c ?? -1;  


此运算符还可用于多个可以为 null 的类型。例如:

  1. int e = null;  
  2. int f = null;  
  3.   
  4. // g = e or f, unless e and f are both null, in which case g = -1.  
  5. int g = e ?? f ?? -1;  

还有一个就是对bool?的运算的说明


bool? 可以为 null 的类型可以包含三个不同的值:truefalsenull

可以为 null 的布尔值类似于 SQL 中使用的布尔变量类型。若要确保由& 和 产生的结果| 运算符与 SQL 中的三值布尔类型一致,提供了以下预定义的运算符:

bool? operator &(bool? x, bool? y)

bool? operator |(bool? x, bool? y)

有个表格说明,这里就不在网上贴了,(太大)

可以去MSDN看看:

连接:点击打开链接

 

最后加一句:有人帮我解释了下为什么两个位null的字符串相加结果会是Empty,我贴下来:

 

  1. string str1 = null;  
  2. string str2 = null;  
  3. string str3 = str1 + str2;  


string在使用+运算时,并没有做复杂的判断和操作,直接是调用了concat方法,连接两个string,并将结果传递给str3。

而concat方法对参数是这样操作的:使用 Empty 字符串替代任何空参数。

所以,str3不可能为null

 

好心网友也帮我解释了下可空类型的运算过程:

  1. int a = null;  
  2. int b = null;  
  3. int c = a + b;  


解释如下:

对上面的过程,c在使用+运算时,对+运算符左右都调用了get_HasValue方法,并对结果取两个bool结果的and操作,得到一个 bool结果。若该结果为假,则将c初始化为null。甚至如果猜的没错,int?的默认值就是null。若该结果为真,对运算符左右均调用 GetValueOrDefault方法,获取两个值,做add运算,将结果传递给c。
 

 

如果有不同意见,欢迎留言,满足下我的好奇心,谢谢。


  1. [SecuritySafeCritical]  
  2. public static string Concat(string str0, string str1)  
  3. {  
  4.  if (string.IsNullOrEmpty(str0))  
  5.  {  
  6.  if (string.IsNullOrEmpty(str1))  
  7.  {  
  8.  return string.Empty;  
  9.  }  
  10.  return str1;  
  11.  }  
  12.  else  
  13.  {  
  14.  if (string.IsNullOrEmpty(str1))  
  15.  {  
  16.  return str0;  
  17.  }  
  18.  int length = str0.Length;  
  19.  string text = string.FastAllocateString(length + str1.Length);  
  20.  string.FillStringChecked(text, 0, str0);  
  21.  string.FillStringChecked(text, length, str1);  
  22.  return text;  
  23.  }  
  24. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多