C# 2.0 Features - Part 2.8 Null-Coalescing Operator

Sep
05
2010
In Categories: .NET | CLR
Tags | |

You can see part 1 here, part 2.1 here, part 2.2 here, part 2.3 here, part 2.4 here, part 2.5 here, part 2.6 here and part 2.7 here.

You will need this a lot when assigning nullable value or object value to one of the value types. We use the "??" to replace "?:" in some cases. Let's see this example

    1    int? x = 10;

    2    x = null;

    3    int y = x ?? 0;

As we can see in this example we use the "??" to say if x is null then y = 0 else y = x. it is the same if we say y = x.HasValue ? x.Value : 0; Although "??" is not descriptive but once you know it you will use it.

Comments

trackback
Technical Architect blog
9/13/2010 2:28:54 PM Permalink

C# 2.0 Features - Part 2.9 Static Classes

C# 2.0 Features - Part 2.9 Static Classes

Comments are closed