What Is Ternary Operator In C#

In this blog, I am going to share with you about Ternary Operator in C#.
 
Why should we use Ternary Operator?
 
Suppose we are creating a program in which we have to compare two values and based on it, execute the statement. Whenever we got that, we have to compare two values. We can do this with if and else, as shown in below program.
  1. using System;  
  2.   
  3. namespace Tutpoint  
  4. {  
  5.     class Program  
  6.     {  
  7.         public static void Compare(int a, int b)  
  8.         {  
  9.             if (a > b)  
  10.             {  
  11.                 Console.WriteLine("A is greater than B");  
  12.             }  
  13.             else  
  14.             {  
  15.                 Console.WriteLine("B is greater than A");  
  16.             }  
  17.         }  
  18.         static void Main(string[] args)  
  19.         {  
  20.             Program.Compare(66, 579);  
  21.             Console.ReadKey();  
  22.         }  
  23.     }  
  24. }  
Output

B is greater than A

If we observe the above code, for comparing two values, we have used '
if-else'. In C#, we have a special decision-making operator called ternary operator which is similar to if-else. The ternary operator compares two values and based on it, returns a value. The above program can be rewritten using the ternary operator as shown below.
  1. using System;  
  2.   
  3. namespace Tutpoint  
  4. {  
  5.     class Program  
  6.     {  
  7.         public static void Compare(int a, int b)  
  8.         {  
  9.             string output = a > b ? "A is greater than B" : "B is greater than A";  
  10.             Console.WriteLine(output);  
  11.         }  
  12.         static void Main(string[] args)  
  13.         {  
  14.             Program.Compare(66, 579);  
  15.             Console.ReadKey();  
  16.         }  
  17.     }  
  18. }  
If we observe this program, the code looks more concise and shorter.
 
What is a Ternary operator?
 
C# has a special decision-making operator named 'Ternary operator' used to compare two values.
 
The Syntax of Ternary operator is,
 
data_type Output_Variable = Conditional Expression? first_statement: second_statement;
 
Here, generally, we use data_type as var. The reason for this is that the ternary operator can return value of any data type.
example,
  1. var output = a > b ? "A is greater than B" : "B is greater than A";      
If the 'Conditional Expression' evaluates to true then return the value of  'first_statement' else for 'second_statement'.
 
Ternary operator always return a value from either 'first_statement' or 'second_statement'
 
With ternary operator, we cannot execute a statement. It must return some value. If we do not assign the ternary operator with a variable or try to execute a statement from 'first_statement' and 'second_statement' then the compiler will produce an error as "Only assignment, call, increment, decrement, and new object expressions can be used as statement"
 
Note
Here, 'first_statement' and 'second_statement' must be one-liner only. We have to be sure before using a ternary operator that there will always be one line of code in 'first_statement' and 'second_statement'. If there will be the chances that code will change in future with lines of code then if-else should be used,
  1. using System;  
  2.   
  3. namespace Tutpoint  
  4. {  
  5.     class Program  
  6.     {  
  7.         public static void Compare(int a, int b)  
  8.         {  
  9.             // Ternary operator must always return a value so it should be assigned with a variable  
  10.             // Also, Statements cannot be executed inside ternary operator  
  11.             //This will produce a compiler error as "Only assignment, call, increment, decrement, and new object expressions can be used as statement"  
  12.             a > b ? Console.WriteLine("A is greater than B") : Console.WriteLine("B is greater than A");  
  13.   
  14.             // Correct way  
  15.             var output = a > b ? "A is greater than B" : "B is greater than A";  
  16.             Console.WriteLine(output);  
  17.         }  
  18.         static void Main(string[] args)  
  19.         {  
  20.             Program.Compare(66, 579);  
  21.             Console.ReadKey();  
  22.         }  
  23.     }  
  24. }  
Nested Ternary operator
 
We can also create nested ternary operator by using the ternary operator on 'First_statement' or 'second_statement'. Using nested ternary operator we can recreate the above program of comparing two numbers with three conditions
  • A is greater than B
  • A is equal to B
  • B is greater than A
  1. using System;  
  2.   
  3. namespace Tutpoint  
  4. {  
  5.     class Program  
  6.     {  
  7.         public static void Compare(int a, int b)  
  8.         {  
  9.             string output = a > b ? "A is greater than B" : a == b ? "A is equal to B" : "B is greater than A";  
  10.             Console.WriteLine(output);  
  11.         }  
  12.         static void Main(string[] args)  
  13.         {  
  14.             Program.Compare(66, 66);  
  15.             Program.Compare(136, 46);  
  16.             Program.Compare(66, 799);  
  17.             Console.ReadKey();  
  18.         }  
  19.     }  
  20. }  
Output

A is equal to B
A is greater than B
B is greater than A

Conclusion
 
'Ternary operator' makes the code shorter and concise. There is no difference between the performance of ternary operator and if-else. It's just about the conditional expression and statements to be executed when the condition goes true or false. If we are sure that code is a one-liner and not going to change in future, go for ternary else not.

I hope this article helps you to understand a bit more about 'Ternary operator'.Thank You & feel free to ask any question or suggestion.
Next Recommended Reading Use Ternary Operator In C# Language