传说中的"int?"--使用可空类型

 

None.gif 1 .IsNullOrEmpty
None.gif确定三个字符串中的每个字符串是有一个值、为空字符串还是为空引用
None.gif
// This example demonstrates the String.IsNullOrEmpty() method
None.gif
using  System;
None.gif
None.gif
class  Sample 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public static void Main() 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif    
string s1 = "abcd";
InBlock.gif    
string s2 = "";
InBlock.gif    
string s3 = null;
InBlock.gif
InBlock.gif    Console.WriteLine(
"String s1 {0}.", Test(s1));
InBlock.gif    Console.WriteLine(
"String s2 {0}.", Test(s2));
InBlock.gif    Console.WriteLine(
"String s3 {0}.", Test(s3));
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public static String Test(string s)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif    
if (String.IsNullOrEmpty(s) == true
InBlock.gif        
return "is null or empty";
InBlock.gif    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
return String.Format("(\"dot.gif{0}\") is not null or empty", s);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

ExpandedBlockStart.gifContractedBlock.gif
/**/ /*
InBlock.gifThis example produces the following results:
InBlock.gif
InBlock.gifString s1 ("abcd") is not null or empty.
InBlock.gifString s2 is null or empty.
InBlock.gifString s3 is null or empty.
InBlock.gif
ExpandedBlockEnd.gif
*/

None.gif

 

None.gif 可空类型可以表示基础类型的所有值,另外还可以表示  null  值。可空类型可通过下面两种方式中的一种声明:
None.gif
None.gifSystem.Nullable
< T >  variable 
None.gif
None.gif
-  或  -
None.gif
None.gifT
?  variable 
None.gif
None.gifT 是可空类型的基础类型。T 可以是包括 
struct  在内的任何值类型;但不能是引用类型。 
None.gif
None.gif有关可能使用可空类型的示例,请考虑普通的布尔变量如何能够具有两个值:
true  和  false 。不存在表示“未定义”的值。在很多编程应用中(最突出的是数据库交互),变量可存在于未定义的状态。例如,数据库中的某个字段可能包含值  true  或  false ,但是它也可能根本不包含值。同样,可以将引用类型设置为  null ,以指示它们未初始化。
None.gif
None.gif这种不一致会导致额外的编程工作,如使用附加变量来存储状态信息、使用特殊值,等等。可空类型修饰符使 C# 能够创建表示未定义值的值类型变量。
None.gif
None.gif可空类型示例
None.gif任何值类型都可用作可空类型的基础。例
None.gif
int ?  i  =   10 ;
None.gif
double ?  d1  =   3.14 ;
None.gif
bool ?  flag  =   null ;
None.gif
char ?  letter  =   ' a ' ;
None.gif
int ? [] arr  =   new   int ? [ 10 ];
None.gif可空类型的每个实例都具有两个公共的只读属性:
None.gif
None.gifHasValue 
None.gif
None.gifHasValue 属于 
bool  类型。当变量包含非空值时,它被设置为  true
None.gif
None.gifValue 
None.gif
None.gifValue 的类型与基础类型相同。如果 HasValue 为 
true ,则说明 Value 包含有意义的值。如果 HasValue 为  false ,则访问 Value 将引发 InvalidOperationException。
None.gif
None.gif在此示例中,HasValue 成员用于在尝试显示变量之前测试它是否包含值。
None.gif
None.gif
int ?  x  =   10 ;
None.gif
if  (x.HasValue)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    System.Console.WriteLine(x.Value);
ExpandedBlockEnd.gif}

None.gif
else
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    System.Console.WriteLine(
"Undefined");
ExpandedBlockEnd.gif}

None.gif
None.gif也可以通过下面的方法测试是否包含值:
None.gif
int ?  y  =   10 ;
None.gif
if  (y  !=   null )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    System.Console.WriteLine(y.Value);
ExpandedBlockEnd.gif}

None.gif
else
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    System.Console.WriteLine(
"Undefined");
ExpandedBlockEnd.gif}

None.gif
None.gif显式转换
None.gif可空类型可强制转换为常规类型,方法是使用强制转换来显式转换或者通过使用 Value 属性来转换。例如:
None.gif
int ?  n  =   null ;
None.gif
None.gif
// int m1 = n;       //  Will not compile.
None.gif
int  m2  =  ( int )n;    //  Compiles, but will create an exception if x is null.
None.gif
int  m3  =  n.Value;   //  Compiles, but will create an exception if x is null.
None.gif

None.gif如果两种数据类型之间定义了用户定义的转换,则同一转换也可用于这些数据类型的可空版本。
None.gif
None.gif隐式转换
None.gif可使用 
null  关键字将可空类型的变量设置为空,如下所示:
None.gif
None.gif
int ?  n1  =   null ;
None.gif从普通类型到可空类型的转换是隐式的。
None.gif
int ?  n2;
None.gifn2 
=   10 ;   //  Implicit conversion.
None.gif

None.gif运算符
None.gif可空类型还可以使用预定义的一元和二元运算符,以及现有的任何用户定义的值类型运算符。如果操作数为空,这些运算符将产生一个空值;否则操作数将使用包含的值来计算结果。例如:
None.gif
int ?  a  =   10 ;
None.gif
int ?  b  =   null ;
None.gif
None.gifa
++ ;          //  Increment by 1, now a is 11.
None.gif
=  a  *   10 ;   //  Multiply by 10, now a is 110.
None.gif
=  a  +  b;    //  Add b, now a is null.
None.gif

None.gif在执行可空类型的比较时,如果其中任一可空类型为 
null ,则比较结果将始终为  false 。因此,一定不要以为由于一个比较结果为  false ,相反的情况就会为  true 。例如:
None.gif
int ?  num1  =   10 ;
None.gif
int ?  num2  =   null ;
None.gif
if  (num1  >=  num2)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    System.Console.WriteLine(
"num1 is greater than or equal to num1");
ExpandedBlockEnd.gif}

None.gif
else
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
// num1 is NOT less than num2
ExpandedBlockEnd.gif
}

None.gif
None.gif
?? 运算符
None.gif
int ?  c  =   null ;
None.gif
None.gif
//  d = c, unless c is null, in which case d = -1.
None.gif
int  d  =  c  ??   - 1 ;
None.gif
None.gif此运算符还可用于多个可空类型。例如:
None.gif
int ?  e  =   null ;
None.gif
int ?  f  =   null ;
None.gif
None.gif
//  g = e or f, unless e and f are both null, in which case g = -1.
None.gif
int  g  =   e  ??  f  ??   - 1 ;
None.gif
None.gif
bool ?  可空类型可以包含三个不同的值: true false  和  null 。它们本身不能用于条件语句,如  if for  或  while
None.gif
None.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值