Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline
Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Trial Issue of Visual Studio Magazine

Hidden Gems in C# 2.0 (Continued)

Nullable Types
Nullable types were developed to simplify creating C# logic that uses data retrieved from databases. Most databases provide a facility to define a missing (or null) value in a given column and record. This missing value is difficult to represent in .NET value types: Which integer value should be used to represent a nonexistent value? There is no one universal answer. So, the C# and CLR teams introduced the idea of a nullable value type.

ADVERTISEMENT

A nullable type is a wrapper around a value type that can represent all the possible values of its underlying type, and can also hold the null value. Nullable types sound simple. But the more you examine the subtleties of the concept, the more complicated they become. The syntax is simple: You append a '?' to a value type to produce a nullable type. (This is a shorthand notation for a generic class, nullable <T>, where T must be a value type.) For example, int?, char?, and bool? all define nullable types for the respective underlying value types. However, because the string class is a reference type, you cannot define a nullable string. Of course, because the string class is a reference type, you don't need to, either.

When you use nullable types, you must remember that the nullable type is not a simple replacement for the value. In fact, nullable types introduce quite a few complications into your program logic. You'd best beware of all the pitfalls before you venture into nullable types where the regular value type would suffice. In these examples, I'll use integer nullables, but any nullable type would exhibit the same problem.

First, a regular integer cannot be assigned the value of a nullable type without a cast, or accessing its underlying Value property:

int? maybeAValue;
int val = maybeAValue.Value;
int val2 = (int ) maybeAvalue;

Both these assignments compile, but a System.InvalidOperationException will be thrown if maybeAValue stores null. That's just extra work. Any time you access the value inside a nullable type, you must check whether the instance has a valid value using the Nullable<T>.HasValue property. In fact, this idiom is common enough that the C# language has included a special syntax to perform assignments with nullable types, the ?? operator.

The ?? operator returns the value of the left operand if the operand has a value, and the right operand otherwise. For instance:

int result = maybeAValue ?? 0;

returns 0 if maybeAValue is null. Otherwise, it returns the value stored in the nullable maybeAValue.

Things get even more complicated when you start boxing and unboxing nullable types. The CLR and C# teams recently made a change to nullable types that creates more cohesive behavior when nullable types are boxed and unboxed. The CTP release that incorporates the change was being released as I was finishing this article, so I have not had the chance to work with the new code yet. Click here for more details.

Miscellaneous
There are a few other additions that warrant mention, but won't affect your daily coding as much. You can now enable and disable warnings inline in code. This will make it easier to get large projects to compile cleanly at higher warning levels. You can selectively disable certain warning conditions after reviewing the specific code that generates the warnings. Similar to the C++ compiler, the syntax is a pre-processor pragma:

#pragma warning disable 3021

C# 2.0 adds the ability to create fixed block arrays, but only in unsafe code blocks. You can use this feature to provide buffers that can be used between managed and unmanaged code layers. For instance, this structure takes 4 bytes on the stack, and the myArray variable is a reference that points to a different location in managed memory:

public struct MyArray
{
  public char MyName[50];
}
Back to top














Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home