Deborah's Developer MindScape






         Tips and Techniques for Web and .NET developers.

July 28, 2009

Coalesce and Ternary Operators

Filed under: C#,VB.NET @ 3:12 pm

Coalesce Operator

The coalesce operator, also called the null coalescing operator, is new in VB 9 and C# 2.0. It helps you work with nulls (Nothing in VB).

The coalesce operator involves two expressions. The first expression must evaluate to a nullable type or reference type. The first expression is the result if the first expression evaluates to a non-null value. The second expression is the result if the first expression evaluates to null (Nothing in VB).

In C#:

string result = value ?? "<Empty>";

?? is the coalesce operator in C#. It is of the general form:

Exression ?? NullResult

In VB:

Dim result As String = If(value, "<Empty>")

The If keyword is used as the coalesce operator in VB. It is of the general form:

If(Expression ?? NullResult)

To use the coalesce operator, the value variable in the above examples must be a nullable type or a reference type. If value is non-null, value is returned. If value is null (Nothing in VB), the second parameter is returned. Here are some examples:

  • value = “CA”                                            result = “CA”
  • value = String.Empty                               result = String.Empty
  • value = null (nothing in VB)                     result = “<Empty>”

Use the coalesce operator any time you need to have special case handling of a null.

Ternary Operator

The ternary operator, also called the conditional operator, is new in VB 9 and has been in C# from the beginning. For a general description of a ternary operator, see this.

The ternary operator involves three expressions (hence the name ternary). The first expression is a Boolean expression that evaluates to true or false. The second expression is the result if the first expression is true. The third expression is the result if the first expression is false.

In C#:

string result = value == null ? "<Empty>" : "<Full>";

? is the ternary operator in C#. It is of the general form:

Condition ? TrueResult : FalseResult

In VB:

Dim result As String = If(value Is Nothing, "<Empty>", "<Full>")

The If keyword is used as the ternary operator in VB. It is of the general form:

If(Condition, TrueResult, FalseResult)

NOTE: In VB, you should always favor the ternary If() syntax over the old IIF syntax, which was inefficient and not recommended for use.

In the examples, if first expression is true, the second expression is returned. If the first expression is false, the third expression is returned. Here are some examples:

  • value = “CA”                                            result = “<Full>”
  • value = String.Empty                               result = “<Full>”
  • value = null (nothing in VB)                     result = “<Empty>”

Use the ternary operator any time you need two different results depending on a Boolean expression.

Enjoy!

4 Comments

  1.   Chuck Berg — July 28, 2009 @ 5:25 pm    Reply

    And the coalesce operator can be concatenated, as follows:

    currentUser = _currentUser ??
    GetWebUserFromSession() ??
    GetWebUserFromTrackingCookie() ??
    CreateNewWebUser();

    Some developers consider this to confusing and difficult to maintain, but I think that over time, people will get used to it and it will become common.

  2.   Ruddy — July 29, 2009 @ 10:58 am    Reply

    As always, very simple and helpful examples that remind us that there is a better way to do things.

    Keep it up.

  3.   Andy Rose — August 21, 2009 @ 3:46 am    Reply

    Completely forgotten about the coalesce operator. Thanks for the reminder. Great blog by the way.

  4.   Cairo Hotels — January 6, 2011 @ 11:31 am    Reply

    if OurHotelName is null everything ok but OurHotelName is empty how can coalesce instead of empty to null.

    COALESCE(@OurHotelName,OurHotelName)

RSS feed for comments on this post. TrackBack URI

Leave a comment

© 2020 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs