Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# 2008 Step by Step.pdf
Скачиваний:
27
Добавлен:
25.03.2016
Размер:
14 Мб
Скачать
☆

150

Part II Understanding the C# Language

Understanding Null Values and Nullable Types

When you declare a variable, it is always a good idea to initialize it. With value types, it is common to see code such as this:

int i = 0; double d = 0.0;

Remember that to initialize a reference variable such as a class, you can create a new instance of the class and assign the reference variable to the new object, like this:

Circle c = new Circle(42);

This is all very well, but what if you don’t actually want to create a new object—perhaps the

purpose of the variable is simply to store a reference to an existing object. In the following code example, the Circle variable copy is initialized, but later it is assigned a reference to another instance of the Circle class:

Circle

c = new Circle(42);

// Some random value, for initializing copy

Circle copy = new Circle(99);

...

 

// copy and c refer to the same object

copy = c;

After assigning c to copy, what happens to the original Circle object with a radius of 99

that you used to initialize copy? Nothing refers to it anymore. In this situation, the runtime can reclaim the memory by performing an operation known as garbage collection. You

will learn about garbage collection in Chapter 14, “Using Garbage Collection and Resource Management.” The important thing to understand for now is that garbage collection is a potentially expensive operation.

You could argue that if a variable is going to be assigned a reference to another object at some point in a program, there is no point initializing it. But this is poor programming practice and can lead to problems in your code. For example, you will inevitably meet the situation where you want to refer a variable to an object only if that variable does not already contain a reference:

Circle

c = new Circle(42);

// Uninitialized !!!

Circle

copy;

...

if (copy == // what goes here?)

copy = c; // copy and c refer to the same object

The purpose of the if statement is to test the copy variable to see whether it is initialized,

but to which value should you compare this variable? The answer is to use a special value called null.

Chapter 8 Understanding Values and References

151

In C#, you can assign the null value to any reference variable. The null value simply means that the variable does not refer to an object in memory. You can use it like this:

Circle c = new Circle(42);

// Initialized

Circle copy = null;

...

 

if (copy == null)

// copy and c refer to the same object

copy = c;

Using Nullable Types

The null value is useful for initializing reference types, but null is itself a reference, and you cannot assign it to a value type. The following statement is therefore illegal in C#:

int i = null; // illegal

However, C# defines a modifier that you can use to declare that a variable is a nullable value

type. A nullable value type behaves in a similar manner to the original value type, but you can assign the null value to it. You use the question mark (?)to indicate that a value type is

nullable, like this:

int? i = null; // legal

You can ascertain whether a nullable variable contains null by testing it in the same way as a reference type:

if (i == null)

...

You can assign an expression of the appropriate value type directly to a nullable variable. The following examples are all legal:

int? i = null;

 

 

 

 

int j = 99;

// Copy a

value type

constant

to a nullable type

i = 100;

i = j;

// Copy a

value type

variable

to a nullable type

You should note that the converse is not true. You cannot assign a nullable value to an ordinary value type variable, so given the definitions of variables i and j from the preceding example, the following statement is not allowed:

j = i;

// Illegal

This also means that you cannot use a nullable variable as a parameter to a method that expects an ordinary value type. If you recall, the Pass.Value method from the preceding

exercise expects an ordinary int parameter, so the following method call will not compile:

int? i = 99;

// Compiler error

Pass.Value(i);

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