.Net Conditional (Tenary) Operator

C♯ 1.0 C♯ 1.2 C♯ 2.0 C♯ 3.0 C♯ 4.0
VB.Net 7.0 VB.Net 7.1 VB.Net 8.0 VB.Net 9.0 VB.Net 10.0
x x x

The conditional operator  takes three operands. It tests the result of the first operand and then returns one of the other two operands based on the result of the first.

C♯ Syntax

condition ? first_expression : second_expression

VB.Net Syntax

IF(condition, first_expression, second_expression)


There is a function in the Microsoft.VisualBasic namepace that acted similarly:

IIf(condition, first_expression, second_expression)

The differences are as follows:

IF Conditional Operator IIf Function
Available in VB.Net 9.0 and above Available in all versions of VB.Net
Determines type of return value based on type of operands Return type is always object
Only evaluates required operands. Always evaluates all operands
Be Sociable, Share!
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *