Advertisement



< Prev
Next >



C# Conditional Operator





In this tutorial, we are going to discuss another important operator provided by C# i.e. the conditional operator. A conditional operator is named so because it returns a value by evaluating a boolean condition. The conditional operator is also called ternary operator because it has three operands, such as:




Syntax of conditional operator -


boolean-condition ? first expression : second expression;






Conditional operator example.


//C# Conditional Operator Example

using System;

class A
{
public static void Main()
{
	//Local variables
	int a=10, b=20;

	
	//A conditional expression with int expressions
	int result1 =a>b ? 10 : 20;
	Console.WriteLine("Result1 is : " + result1);


	//A conditional expression with an int and a double expression, 
	//hence result of condition expression should be stored in a double
	double result2 = a>b ? 10 : 100.50;
	Console.WriteLine("Result2 is : " + result2);
	

	//A conditional expression with char expressions
	char ch = a>b ? 'y' : 'n';
	Console.WriteLine("Result3 is : " + ch);
	

	//A conditional expression with int expression
	result1 = 100>=99 ? 100 : 99;
	Console.WriteLine("Result4 is : " + result1);
}
}
Output
Result1 is 20
Result2 is 100.5
Result3 is n
Result4 is 100





  • The first expression of a conditional operator cannot be left blank

  • //C# Conditional Operator
    
    using System;
    
    class A
    {
    public static void Main()
    {
    	//Local variables
    	int a=10, b=20;
    
    	int result = a>b ? 0 : 10;
    	Console.WriteLine("Result1 is : " + result);
    
    	//Intentionally leaving the first expression of the conditional operator 
    	//This will throw a compile error.
    	char ch = a<b ? : 'n'; 	
    	Console.WriteLine("Result2 is : " + ch);
    }
    }


    Compile Error:


    A.cs(16,18): error CS1525: Invalid expression term ':'

    In the last code, we have intentionally left out the first expression and the compile error has been reported.




  • The second expression of a conditional operator cannot be left blank either

  • Not specifying the second expression also results in a compile error.

    //C# Example of conditional operator, the second expression cannot be left blank
    
    using System;
    
    class A
    {
    public static void Main()
    {
    	//A conditional expression with the missing second expression
    	result = 9<2 ? 9 : ;
    	
    	Console.WriteLine("Result2 is : " + result);
    }
    }


    Output-


    A.cs(10,21): error CS1525: Invalid expression term ';'

    In the last code, we have intentionally left out the second expression and the compiler has thrown an error, therefore, neither the first, nor the second expression of a conditional operator can be left out.


    Advertisement




  • An expression of a conditional operator can even be a method call.
  • Any expression(either the first or the second expression) of a conditional operator could even be a method call but this method must return a value.

    //C# An expression of a conditional operator can be a method call.
    
    using System;
    
    class A
    {
    public char fun()
    {
    	return 'y';
    }
    
    public static void Main()
    {
    	//Creating an object of class A	
    	A ob = new A();
    
    	// Calling function fun() in the first expression of a conditional expression
    	char result = 100>99? ob.fun() : 'n'; 
    
    	//Printing the value returned by the conditional expression
    	Console.WriteLine("Result is " + result);
    }
    }
    


    Output-


    Result is y

    In the last code, the first expression of the conditional operator is a call to a method fun(), which has returned us a char value y.



    Please share this article -





    < Prev
    Next >
    < C# Logical Operators
    C# Array >



    Advertisement

    Please Subscribe

    Please subscribe to our social media channels for daily updates.


    Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




    Advertisement



    Notifications



    Please check our latest addition

    C#, PYTHON and DJANGO


    Advertisement