Ternary operator in VB.NET

This blog defines the Ternary operator in VB.NET.

Ternary operator

The general syntax of the Ternary operator in VB.NET.

If(question, expression1, expression2)
Here question(?) is the condition if condition is true, then expression1 will be execute otherwise expression2 will be execute.

The general syntax of the Ternary operator in C#.NET.

question ? expression1 : expression2

It can be rewritten the above Syntax :

if (question =true){
expression1;
}else {
expression2;
}
For example
C# code

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ConsoleApplication42

{

    class Program

    {

        static void Main(string[] args)

        {

            int i = 3;

            int j = 2;

            Console.WriteLine(i > j ? i : j);

        }

    }

}

VB code

Module Module1

    Sub Main()

        Dim i As Integer = 3

        Dim j As Integer = 2

        Console.WriteLine(If(i > j, i, j))

    End Sub

End Module

 

OUTPUT


b1.gif