
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Creating a Value Type That Can be Initialized to Null
|
239
current point in the list? If so, you know the target object is at an index somewhere
in the list above the current index. If not, the object is at an index somewhere in the
list below the current index. The binary search algorithm keeps asking this question
until the object is found. In contrast, a linear search starts at the first element in a list
and determines if that object is the one you are looking for. If not, the search contin-
ues to the next element in the list. This operation keeps repeating until the object is
found in the list.
See Also
See the “LinkedList<T> Class” topic in the MSDN documentation.
4.7 Creating a Value Type That Can be Initialized
to Null
Problem
You have a variable that is a numeric type, which will hold a numeric value obtained
from a database. The database may return this value as a
null. You need a simple
clean way to store this numeric value, even if it is returned as a
null.
Solution
Use a nullable value type. There are two ways of creating a nullable value type. The
first way is to use the
? type modifier:
int? myDBInt = null;
The second way is to use the Nullable<T> generic type:
Nullable<int> myDBInt = new Nullable<int>( );
Discussion
Essentially both of the following statements are equivalent: