【基础语言学习】C#中的可空类型运算(Nullable<T>)------T?

来源:互联网 发布:创业软件股份有限公司 编辑:程序博客网 时间:2024/06/05 03:31

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

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Null值相加{    class Program    {        static void Main(string[] args)        {            //两个null字符串相加            string str1 = null;            string str2 = null;            string str3 = str1 + str2;            Console.WriteLine("==>两个为null的字符串相加的结果:\n==>" +                (str3 == null ? "null" : (str3.ToString() == string.Empty ? "string.Empty --> 居然为长度为0的字符串。" : str3.ToString())));            Console.WriteLine("==>相加后字符串[str3]的长度为:" + str3.Length + System.Environment.NewLine);            Console.WriteLine("==>我操,这是什么原因??????\n==>按任意键继续......\n\n=====================\n");            //一个null字符串和一个非null字符串相加            string str11 = null;            string str22 = "test";            string str33 = str11 + str22;            Console.WriteLine("==>一个null字符串和一个非null字符串的结果:\n==>" +                              (str3 == null ? "null" : (str33.ToString() == string.Empty ?                                "string.Empty --> 居然为长度为0的字符串。" : str33.ToString())));            Console.WriteLine("==>相加后字符串[str33]的长度为:" + str33.Length + System.Environment.NewLine);            Console.WriteLine(@"==>我操,对于引用类型来说,一个位null的引用类型和一个不为null的引用类型相加,结果为不为null。\n                                ==>按任意键继续......\n\n=====================\n");            //两个为null的可空整形相加            int? a = null;            int? b = null;            int? c = a + b;            Console.WriteLine("==>两个为[null]的可空[int?]类型相加的结果:\n" +                                 (c == null ? "==>结果为null" : "没猜出来是什么结果") + System.Environment.NewLine);            Console.WriteLine("==>我操!!!!!!!!!!!\n==>按任意键继续......\n\n");            //一个为null可空的[int?]类型与一个[int]相加            int? na = null;            int nb = 11;            int? nc = na + nb;            Console.WriteLine("==>一个为null的可空[int?]类型与一个[int]相加的结果:\n"                                 + (nc == null ? "==>结果为null" : nc.Value.ToString()) + System.Environment.NewLine);            Console.WriteLine("==>我操,值为null的可控类型与不为null的可控类型相加其值为null.\n==>按任意键继续......\n\n");            Console.ReadKey();        }    }}


结果截图:

 

后来查了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

int? x = 10;if (x.HasValue){    System.Console.WriteLine(x.Value);}else{    System.Console.WriteLine("Undefined");}


 测试代码:

int? y = 10;if (y != null){    System.Console.WriteLine(y.Value);}else{    System.Console.WriteLine("Undefined");}


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

 

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

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

 

int? n1 = null;


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

int? n2;n2 = 10;  // Implicit conversion.


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

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

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

 

int? num1 = 10;int? num2 = null;if (num1 >= num2){    Console.WriteLine("num1 is greater than or equal to num2");}else{    // This clause is selected, but num1 is not less than num2.    Console.WriteLine("num1 >= num2 returned false (but num1 < num2 also is false)");}if (num1 < num2){    Console.WriteLine("num1 is less than num2");}else{    // The else clause is selected again, but num1 is not greater than    // or equal to num2.    Console.WriteLine("num1 < num2 returned false (but num1 >= num2 also is false)");}if (num1 != num2){    // This comparison is true, num1 and num2 are not equal.    Console.WriteLine("Finally, num1 != num2 returns true!");}// Change the value of num1, so that both num1 and num2 are null.num1 = null;if (num1 == num2){    // The equality comparison returns true when both operands are null.    Console.WriteLine("num1 == num2 returns true when the value of each is null");}/* Output: * num1 >= num2 returned false (but num1 < num2 also is false) * num1 < num2 returned false (but num1 >= num2 also is false) * Finally, num1 != num2 returns true! * num1 == num2 returns true when the value of each is null */


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

 

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


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

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

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


bool? 可以为 null 的类型可以包含三个不同的值:true、false 和null。

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

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

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

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

可以去MSDN看看:

连接:点击打开链接

 

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

 

string str1 = null;string str2 = null;string str3 = str1 + str2;


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

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

所以,str3不可能为null

 

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

  int? a = null;  int? b = null;  int? c = a + b;


解释如下:

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

 

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


[SecuritySafeCritical]public static string Concat(string str0, string str1){ if (string.IsNullOrEmpty(str0)) { if (string.IsNullOrEmpty(str1)) { return string.Empty; } return str1; } else { if (string.IsNullOrEmpty(str1)) { return str0; } int length = str0.Length; string text = string.FastAllocateString(length + str1.Length); string.FillStringChecked(text, 0, str0); string.FillStringChecked(text, length, str1); return text; }}


 

原创粉丝点击