...

/

C# Ternary Expression

C# Ternary Expression

This lesson discusses ternary operators in detail including compound ternary expressions using examples

Ternay Operator

This is a short way of representing conditional statement in C#.

Ternary operator has one boolean expression, and returns one of two values depending on the value of a Boolean expression.

Syntax

Here’s the syntax

Press + to interact
condition ? expression_if_true : expression_if_false;

Example

Let’s take a look at an example which uses ternary operators.

Press + to interact
using System;
class TernaryExample
{
static void Main()
{
string name = "Frank"; //change this name to see the false condition execute
Console.WriteLine(name == "Frank" ? "The name is Frank" : "The name is not Frank");
}
}

Code Explanation

In the code above

...