Most people try to make the argument that C Sharp doesn't have a lot of the helper functions like VB.NET. That may be true in some cases but not all. In VB developers have the luxury of using functions like IIF which accepts a boolean expression and two object parameters for true part and false part and returns the corresponding string. Yes, it is true that C# doesn't have an IIF function but us C# developers have the same functionality just different syntax. It is called the Conditional Operator. The syntax is as follows.
logical-or-expression ? expression : conditional-expression
Below is an example of the C# version of an IIF Function.
The conditional operator (? :) is a ternary operator (it takes three operands). The conditional operator works as follows:
- The first operand is implicitly converted to bool. It is evaluated and all side effects are completed before continuing.
- If the first operand evaluates to true (1), the second operand is evaluated.
- If the first operand evaluates to false (0), the third operand is evaluated.