Over a million developers have joined DZone.

What's The IIF In C#

DZone's Guide to

What's The IIF In C#

· ·
Free Resource
C# has the "?" ternary operator, like other C-style languages. However, this is not perfectly equivalent to iif. There are two important differences.

To explain the first, this iif() call would cause a DivideByZero exception even though the expression is true because iif is just a function and all arguments must be evaluated before calling:

iif(true, 1, 1/0)

Another way, iif does not short circuit in the traditional sense, as your question indicates. On the other hand, this ternary expression does and so is perfectly fine:


The other difference is that iif is not type safe. It accepts and returns arguments of type object. The ternary operator uses type inference to know what type it's dealing with

Eclipse IDE and Eclipse

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}