Updated March 24, 2023
Introduction to Conditional Operators in C#
Conditional operators in C# as the name suggest referring to the use of three operands in a C# program. The operands used to denote a condition, the value if the condition is true and the value if the condition is false. The symbol used to represent the conditional operator in C# is ‘? : ’ (the quotes are to be ignored as they are not part of the symbol and used to enclose the symbol distinguish).
Syntax:
condition_expression ? first_expression : second_expression;
How to Define Conditional Operators?
The first operand is specified before the? and contains the definition of evaluating condition expression. Care needs to be taken while defining the expression such that the evaluation condition should always result in a bool result. No specifying a Boolean expression or specifying a faulty expression shall result in a compilation error. The second expression specified before the: symbol and holds the definition or assignment in case the condition defined in the first expression evaluates to true. Thus if the return value determined in the first expression is true the second operand is evaluated.
The third operand in the definition contains the definition of the expression in case the conditional result of the first operand evaluates to false. Alternatively, the conditional operators are known as ternary operators or inline if operators. The major use of the conditional operators in C# is found as an alternative for the if-else loop where this is used to reduce the size of the code block. The other major advantage of the conditional operator is that it translates the compilation flow in terms of branch statements which reduces the use of nested if statement required.
The conditional operator follows the right association principle i.e. the operations are grouped from right to left. Also, the compilation time is highly reduced as the conditional operator only evaluates a single operand value expression. The value of the second and third operand is restricted to the assignment, increment and decrement functions.
Examples of Conditional Operators in C#
Let us try to understand the approach to traditional C# programming with the conditional operator.
Example #1
Let us first try a regular if else statement:-
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int x = 10, y = 25;
string result1;
if (x > y)
{
result1 = "value of x is greater than y";
}
else
{
result1 = "value of x is less than y";
}
Console.WriteLine(result1);
Console.ReadLine();
}
}
}
Output:
The program above illustrates a simple if-else statement that compares the value of two variables x and y and prints a result as per the value assigned to them and upon evaluation of the condition, x> y.
Example #2
Let us try to replicate the program above using a conditional operator →.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int x = 10, y = 25;
string result1;
//using the Ternary Operator (?:) for the comparison of x and y variable
result1 = (x > y) ? "value of x is greater than y" : "value of x is less than y";
Console.WriteLine(result1);
Console.ReadLine();
}
}
}
Output:
Now we shall identify the major use of the conditional operator where it is used as a means to simplify the complex if-else if nests by providing branch or nested criteria of its own which is much less complex when compared to if-else if loop.
Example #3
To understand the same let us first consider a regular if-else if program:-
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string comparisonResult;
int a = 25, b = 50;
if (a < b)
{
comparisonResult = "value of a is less than b ";
}
else if (a > b)
{
comparisonResult = " value of a is more than b";
}
else
{
comparisonResult = "a and b are both equal in value ";
}
Console.WriteLine(comparisonResult);
Console.ReadLine();
}
}
}
Output:
Example #4
Now we shall see how the same logic is implemented in C# using the conditional operators.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string comparisonResult;
int a = 25, b = 50;
//Nested Ternary Operator (?:)
comparisonResult = (a < b) ? "value of a is less than b" : (a > b) ? "value of a is more than b" : "a and b are both equal in value";
Console.WriteLine(comparisonResult);
Console.ReadLine();
}
}
}
Output:
Thus we see the difference in both as the complexity of the if-else if statements are highly reduced by the use of nested conditional operators. Also understanding the flow of the program we find out the cursor flow as the right association principle
(a < b) ? "value of a is less than b" : (a > b) ? "value of a is more than b" : "a and b are both equal in value";
The code blocks are denoted using a separate color as for a statement like A? B : C? D : E the same gets evaluated as (A ? B : C) ? D : E.
Conclusion
Conditional operators are three operand operators used in evaluation and assignment operations having separate assignments for the evaluated value of the conditional operand i.e either true or false. The conditional operators follow the right association principle which refers to the point that the operations are grouped from right to left. The conditional operator cannot be overloaded using any user-defined type and restricted to bool value for evaluation. The conditional operators provide the most suited replacement for lengthy if-else if statements and also reducing the compiling time by providing an opportunity for branched statement evaluation.
Recommended Articles
This is a guide to the Conditional Operators in C#. Here we discuss syntax and how to define Conditional Operators in C# along with example and code implementation. You may also look at the following articles to learn more-