C#变量定义中含问号的用法 [ZT] 例 int?

http://blog.sina.com.cn/s/blog_731a5cd40100pbrt.html

 

今天做项目,发现框架里竟然在声明 int 类型的变量的时候加了问号,甚为惊讶,遂在网络搜索其用法,看到以下一个帖子。
***讲:三天不学习,追不上***阿
 
WCF的lab做着做着,发现了一个decimal? dealValue = null;的定义。大惑不解,问周围人亦不得解。只能msdn search了一把,发现真的落后于时代了。原来是C#2.0的新特性,为了避免基元类型置null必须要装箱或者自建struct而引入的。

因为在各大搜索引擎上搜索不得解,所以特意编制“C#变量定义中含问号的用法”这一标题,将用法列于其下,希望有助于别人。

Nullable Types in C#

One of the "late breaking" features in C# 2.0 is what is known as "Nullable Types". The details can be found in the C# 2.0 language spec.

Nullable types address the scenario where you want to be able to have a primitive type with a null (or unknown) value. This is common in database scenarios, but is also useful in other situations.

In the past, there were several ways of doing this:

  • A boxed value type. This is not strongly-typed at compile-time, and involves doing a heap allocation for every type.
  • A class wrapper for the value type. This is strongly-typed, but still involves a heap allocation, and the you have to write the wrapper.
  • A struct wrapper that supports the concept of nullability. This is a good solution, but you have to write it yourself.

To make this easier, in VS 2005, we're introducing a new type named "Nullable", that looks something like this (it's actually more complex than this, but I want to keep the example simple):

struct Nullable<T>{ public bool HasValue; public T Value; }

You can use this struct directly, but we've also added some shortcut syntax to make the resulting code much cleaner. The first is the introduction of a new syntax for declaring a nullable type. Rather than typing:

Nullable<int> x = new Nullable<int>(125);

I can write:

int? x = 125;

which is much simpler. Similarly, rather than needed to write a null test as:

if (x.HasValue) {...}

you can use a familiar comparison to null:

if (x != null) {...}

Finally, we have support to make writing expressions easier. If I wanted to add two nullable ints together and preserve null values, if I didn't have language support, I would need to write:

Nullable<int> x = new Nullable<int>(125); Nullable<int> y = new Nullable<int>(33); Nullable<int> z = (x.HasValue && y.HasValue) ? 
new Nullable<int>(x.Value + y.Value) : Nullable<int>.NullValue;

At least I think that's what I'd have to write - it's complex enough that I'm not sure this code works. This is ugly enough that it makes using Nullable without compiler support a whole lot of work. With the compiler support, you write:

int? x = 125; int? y = 33; int? z = x + y;
而后又惊异发现居然还有??操作符的存在,感叹原来自己已经落后不止一个时代,也一并贴个用法于下:
 The ?? operator returns the left-hand operand if it is not null, or else it returns the right operand.
 
A nullable type can contain a value, or it can be undefined. 
The ?? operator defines the default value to be returned when                                             
If you try to assign a nullable type to a non-nullable type without using the ?? operator, 
you will generate a compile-time error. 
If you use a cast, and the nullable type is currently undefined, an InvalidOperationException exception will be thrown. 
 
 
 
 
 
 
 
 
 
 
 
// nullable_type_operator.cs
using System; class MainClass{ static int? GetNullableInt(){ return null;} static string GetStringValue() { return null;} static void Main() { // ?? operator example. int? x = null; // y = x, unless x is null, in which case y = -1. int y = x ?? -1; // Assign i to return value of method, unless // return value is null, in which case assign // default value of int to i. int i = GetNullableInt() ?? default(int); string s = GetStringValue(); // ?? also works with reference types. // Display contents of s, unless s is null, // in which case display "Unspecified". Console.WriteLine(s ?? "Unspecified");}}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值