Tag Archives: ISNULL

C# has an ISNULL function, its called ??

Just a very short and quick post. For a while now .Net has had nullable types. Take for example an integer, instead of declaring it as:

int x = 0;

you declare it as:

int? x = 0;

Now in coding examples all over the web I’m seeing the same logic and its driving me nuts!

if(x == null){
    x = -1;
}

There is a much shorter route and that is:

x = x ?? -1;

The “??” operator works exactly the same way an ISNULL function call works in SQL. It evaluates the variable on the left of the operator to ensure its not null (or undefined as some people call it), and if it is, it uses the value on the right of the ?? operator. Quick and easy.