What's The IIF In C#
Join the DZone community and get the full member experience.
Join For FreeC# 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:
(true)?1:1/0;
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.
Comments