Using Nullable Types in C#(1)



With the newest standard for the C# language, there is now support for nullable data types. This small change could be a huge help for those who deal with databases containing fields that are optional. Nullable data types can also be helpful in other situations as well.


Defining Nullable Types

Defining a nullable type is very similar to defining the equivalent non-nullable type. The difference is in the use of the ? type modifier. To define an integer, you normally would do a simple declaration:

int myInt = 1;

To make myInt able to store a null value, you would declare it as such:

int? myNullableInt = 1;

As you can see, these two variables look as though they are the same. The nullable version, however, is much different. The nullable version is actually a structure that combines a value type with a flag to indicate whether the value is null. Additionally, a nullable type has two publicly readable properties, HasValue and value. HasValue is a bool that is true if there is a value stored; otherwise, it is false if the variable is null. If HasValue is true, you can get the value of the variable. If it is false and you attempt to get the value, an exception will be thrown.

null is now a keyword for C#. Additionally, it can be assigned to a nullable variable. The following are two valid assignments for a nullable variable:

double? myDouble = 3.14159;
double? myOtherDouble = null;

As you can see, myDouble is assigned a value, but could also be assigned null. In the second statement, myOtherDouble is initialized to contain a null value—something you can't do with a non-nullable type.

Using a Nullable Type

A nullable type can be used in the same way that a regular value type can be used. In fact, implicit conversions are built in for converting between a nullable and non-nullable variable of the same type. This means you can assign a standard integer to a nullable integer and vice versa:

int? nFirst = null;
int  Second = 2;

nFirst = Second;    // Valid
nFirst = 123;       // Valid
Second = nFirst;    // Also valid

nFirst = null;      // Valid
Second = nFirst;    // Exception, Second is nonnullable.

In looking at the above statements, you can see that a nullable and nonnullable variable can exchange values as long as the nullable variable does not contain a null. If it contains a null, an exception is thrown. To help avoid throwing an exception, you can use the nullable's HasValue property:

if (nFirst.HasValue) Second = nFirst;

As you can see, if nFirst has a value, the assignment will happen; otherwise, the assignment is skipped.

Using Operators with Nullable Values: Lifted Operators

In addition to the automatic conversions between a nullable and non-nullable variable of the same value type, there also are changes with the operators to allow them to work with nullable and non-nullable values. These operators are called lifted operators.

Consider the following code:

int ValA = 10;
int? ValB = 3;

int? ValC = ValA * ValB;

What is stored in Val C? The value of 30 would be stored into ValC. The standard operators have been modified so that they "lift" the non-nullable values to being nullable, thus allowing the standard operations to work. Now, consider the following change:

int ValA = 10;
int? ValB = null;

int? ValC = ValA * ValB;

What would ValC contain this time? ValC would contain null. In the case where either operand is null, the result of a lifted operation also will be null. Even if you were doing addition or subtraction, it would still be null. So, ValA + ValB using the above values would result in null, not 10.

What if ValC were not a nullable type? What does the following do then?

int ValA = 10;
int? ValB = 3;

int ValC = ValA * ValB;    // ValC not nullable

This code would actually throw an exception. The result of ValA * ValB is null and a null can't be assigned to a non-nullable type. As such, an exception is thrown.

转载于:https://www.cnblogs.com/morgen/archive/2005/08/23/220888.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值