Sunday, September 28, 2008

Null-coalescing operator in .NET Framework

The post Declaring variables as nullable in c# describes how nullable value types can be declared used in C#.

.NET Framework also provides the ?? operator, called the null-coalescing operator, and its primary use is to define a default value for a nullable value types as well as reference types. It returns the left-hand operand if it is not null; otherwise it returns the right operand.

As an example, if the following code is run, the intValue will be 1, since nullInt is null.

Nullable<int> nullInt = null;

int intValue = nullInt ?? 1;

1 comment:

Anastácio Soberbo said...

Hello, I like this blog.
Sorry not write more, but my English is not good.
A hug from Portugal