C# Ternary Operator (?:) with Examples

  Keywords : C# decision making operator (?:) is called a ternary operator with examples, C# ternary operator (?:) is the short version of if…else statement with examples

Here we will learn ternary operator (?:) in c# with examples and how to use conditional ternary operator and nested ternary operator in c# to perform if else and if…else if condition in one line with examples.

C# Ternary Operator

In c#, Ternary Operator is a decision making operator (?:) and it is a substitute of if…else statement in c# programming language.

 

By using Ternary Operator, we can replace multiple lines of if…else statement code into single line in c# programming language.

 

The Ternary operator we will help you to execute the statements based on the defined conditions using decision making operator (?:).

Syntax of C# Ternary Operator

In c#, the Ternary Operator will always work with 3 operands. Following is the syntax of defining a Ternary Operator in c# programming language.

 

condition_expression ? first_expression : second_expression; 

If you observe above Ternary Operator syntax, the conditional operator (?:) will return only one value from the defined expressions either first_expression or second_expression based on the value of condition.

 

In c#, the Ternary Operator (?:) will work like as follow.

 

  • In Ternary Operator, the condition expression must be evaluated to either true or false. If condition is true, the first_expression result returned by the ternary operator. 
  • In case, if condition is false, then the second_expression result returned by the operator.

As said earlier, the Ternary Operator (?:) is a substitute of if…else statement in c# programming language. For example, we can replace following if…else statement with Ternary Operator (?:) like as shown following.

 

int x = 5, y = 20;

string result;

 

// if...else statement

if (x > y)

{

result = "x greater than y";

}

else {

result = "x less than y";

}

 

//Ternary Operator (?:) statement

result = (x > y) ? "x greater than y" : "x less than y";

If you observe above example, we simplified if…else condition by replacing multiple lines of if…else condition code with Ternary Operator (?:) in c# programming language.

 

Now we will see the complete example of Ternary operator (?:) in c# programming language.

C# Ternary Operator Example

Following is the example of using Ternary Operator (?:) in c# programming language.

 

using System;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            int x = 5, y = 20;

            string result;

            //Ternary Operator (?:)

            result = (x > y) ? "x value greater than y" : "x value less than y";

            Console.WriteLine(result);

            Console.WriteLine("Press Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe above code, we used a Ternary Operator (?:) to evaluate expression (x > y) to show the result based on our requirements.

 

When we execute above c# program, we will get the result like as shown below.

 

C# Ternary Operator Example Result

 

This is how we can use Ternary Operator (?:) as a substitute of if…else statement in c# programming language.

C# Nested Ternary Operator

In c#, we can create a Nested Ternary Operator by including multiple conditional expressions as a second or third part of expressions in ternary operator and these nested ternary operators will help us to replace if…else if statements in c# programming language.

 

Following is the example of replacing if…else if statement with nested ternary operator in c# programming language.

 

int x = 20, y = 20;

// If...else If Statement

string result;

if (x > y)

{

result = "x value greater than y";

}

else if (x < y)

{

result = "x value less than y";

}

else {

result = "x value equals to y";

}

//Nested Ternary Operator (?:)

result = (x > y) ? "x value greater than y" : (x < y) ? "x value less than y" : "x value equals to y";

If you observe above code, we are able to replace multiple lines of if…else if code with single line of nested ternary operator based on our requirements.

 

In c#, the conditional operator is a right associative so the expression a ? b : c ? d : e; evaluated as a ? b : (c ? d : e), not as (a ? b : c) ? d : e.

C# Nested Ternary Operator Example

Following is the example of defining a nested ternary operator in c# programming language.

 

using System;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            int x = 20, y = 20;

            string result;

            //Nested Ternary Operator (?:)

            result = (x > y) ? "x value greater than y" : (x < y) ? "x value less than y" : "x value equals to y";

            Console.WriteLine(result);

            Console.WriteLine("Press Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

When we execute above c# program, we will get the result like as shown below.

 

C# Nested Ternary Operator Example Result

 

This is how we can implement nested ternary operator in c# programming language to replace if…else if statements based on our requirements.