in , ,

C# ternary (? :) Operator

csharp ternary Operator
csharp ternary Operator

In this tutorial, we will learn about C# ternary operator and how to use it to control the flow of program.

Ternary operator are a substitute for if…else statement. So before you move any further in this tutorial, go through C# if…else statement (on the off chance that you haven’t).

C# incorporates a decision-making operator ?: which is known as the conditional operator or ternary operator. It is the short type of the if else conditions.

The syntax of ternary operator is:

Condition ? Expression1 : Expression2;

The ternary operator works as follows:

• If the expression expressed by Condition is true, the result of Expression1 is returned by the ternary operator.

• If it is false, the result of Expression2 is returned.

For instance, we can replace the accompanying code

if (number % 2 == 0)
{
	isEven = true;
}
else
{
	isEven = false;
}

With

isEven = (number % 2 == 0) ? true : false ;

For what reason is it called ternary operator?

This operator takes 3 operand, henceforth called ternary operator.


Example 1: C# Ternary Operator

using System;

namespace Conditional
{
	class Ternary
	{
		public static void Main(string[] args)
		{
			int number = 2;
			bool isEven;

			isEven = (number % 2 == 0) ? true : false ;  
			Console.WriteLine(isEven);
		}
	}
}

At the point when we run the program, the output will be:

True

In the above program, 2 is assigned to a variable number. At that point, the ternary operator is used to check if number is even or not.

Since, 2 is even, the expression (number % 2 == 0) returns true. We can likewise use ternary operator to return numbers, strings and characters.

Rather than putting away the return value in variable isEven, we can straightforwardly print the worth returned by ternary operator as,

Console.WriteLine((number % 2 == 0) ? true : false);

When to use ternary operator?

Ternary operator can be used to replace multi lines of code with a single line. In any case, we shouldn’t abuse it.

For instance, we can replace the accompanying if..else if code

if (a > b)
{
	result = "a is greater than b";
}
else if (a < b)
{
	result = "b is greater than a";
}
else
{
	result = "a is equal to b";
}

with a single line of code

result = a > b ? "a is greater than b" : a < b ? "b is greater than a" : "a is equal to b";

As should be obvious, the use of ternary operator may diminish the length of code however it makes us hard to comprehend the logic of the code.

Subsequently, it’s smarter to possibly use ternary operator to replace straightforward if else statements.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

csharp switch Statement

C# switch Statement

csharp Bitwise Operators

C# Bitwise and Bit Shift Operators