Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
16
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 3 TYPES, STORAGE, AND VARIABLES

Nullable Types

There are situations, particularly when working with databases, where you want to indicate that a variable does not currently hold a valid value. For reference types, you can do this easily, by setting the variable to null. When you define a variable of a value type, however, its memory is allocated whether or not its contents have any valid meaning.

What you would like in this situation is to have a Boolean indicator associated with the variable, so that when the value is valid, the indicator is true, and when the value is not valid, the indicator is false.

Nullable types allow you to create a value type variable that can be marked as valid or invalid so that you can make sure a variable is valid before using it. Regular value types are called non-nullable types.

Creating a Nullable Type

A nullable type is always based on another type, called the underlying type, that has already been declared.

You can create a nullable type from any value type, including the predefined, simple types.

You cannot create a nullable type from a reference type or from another nullable type.

You do not explicitly declare a nullable type in your code. Instead, you declare a variable of a nullable type. The compiler implicitly creates the nullable type for you.

To create a variable of a nullable type, simply add a question mark to the end of the name of the underlying type, in the variable declaration. Unfortunately, this syntax makes it appear that you have a lot of questions about your code.

For example, the following code declares a variable of the nullable int type. Notice that the suffix is attached to the type name—not the variable name.

Suffix

int? myNInt = 28;

The name of the nullable type includes the suffix.

With this declaration statement, the compiler takes care of both producing the nullable type and creating the variable of that type.

46

CHAPTER 3 TYPES, STORAGE, AND VARIABLES

Using a nullable type is almost the same as using a variable of any other type. Reading a variable of a nullable type returns its value. You must, however, make sure that the variable is not null. Attempting to read the value of a null variable produces an exception.

Like any variable, to retrieve its value, you just use its name.

To check whether a nullable type has a value, you can compare it to null.

Compare to null

if ( myInt1 != null )

Console.WriteLine("{0}", myInt1);

Use variable name

Both sets of code produce the following output:

15

You can easily convert between a nullable type and its corresponding non-nullable type. We’ll go into conversions in detail in Chapter 18, but the important points for nullable types are the following:

There is an implicit conversion between a non-nullable type and its nullable version. That is, no cast is needed.

There is an explicit conversion between a nullable type and its non-nullable version.

For example, the following lines show conversion in both directions. In the first line, a literal of type int is implicitly converted to a value of type int? and is used to initialize the variable of the nullable type. In the second line, the variable is explicitly converted to its non-nullable version.

int?

myInt1

=

15;

//

Implicitly

convert

int to int?

int

regInt

=

(int) myInt1;

//

Explicitly

convert

int? to int

47

CHAPTER 3 TYPES, STORAGE, AND VARIABLES

Assigning to a Nullable Type

You can assign three kinds of values to a variable of a nullable type:

A value of the underlying type

A value of the same nullable type

The value null

The following code shows an example of each of the three types of assignment:

int? myI1, myI2, myI3;

 

 

myI1 = 28;

//

Value of underlying type

myI2

=

myI1;

//

Value of nullable type

myI3

=

null;

//

Null

Console.WriteLine("myI1: {0}, myI2: {1}", myI1,

myI2);

This code produces the following output:

myI1: 28, myI2: 28

In Chapter 25, when you have a clearer understanding of C#, I'll explain the finer points of nullable types.

48

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]