33 out of 57 rated this helpful - Rate this topic

?: Operator (C# Reference)

The conditional operator (?:) returns one of two values depending on the value of a Boolean expression. The conditional operator is of the form

condition ? first_expression : second_expression;

If condition is true, first expression is evaluated and becomes the result; if false, the second expression is evaluated and becomes the result. Only one of two expressions is ever evaluated.

Calculations that might otherwise require an if-else construction can be expressed more concisely and elegantly with the conditional operator. For example, to avoid a division by zero in the calculation of the sin function you could write either

if(x != 0.0) s = Math.Sin(x)/x; else s = 1.0;

or, using the conditional operator,

s = x != 0.0 ? Math.Sin(x)/x : 1.0;

The conditional operator is right-associative, so an expression of the form

a ? b : c ? d : e

is evaluated as

a ? b : (c ? d : e)

not

(a ? b : c) ? d : e

The conditional operator cannot be overloaded.

// cs_operator_conditional.cs
using System;
class MainClass
{
    static double sinc(double x) 
    {
        return x != 0.0 ? Math.Sin(x)/x : 1.0;
    }

    static void Main() 
    {
        Console.WriteLine(sinc(0.2));
        Console.WriteLine(sinc(0.1));
        Console.WriteLine(sinc(0.0));
    }
}
 
0.993346653975306
0.998334166468282
1
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Example for those who stopped at algebra
Here's an example for those who do not know sines and cosines, since that should not be a prerequisite for understanding the conditional operator:

int a,b;
string result;
a = Int16.Parse(TextBox1.Text);
b = Int16.Parse(TextBox2.Text);

// result = conditional test ? if true : if false;
result = a > b ? "a is larger" : "a is not larger";


Edit by SJ at MSFT: You can also get a simpler example in the current version of the topic, here: http://msdn.microsoft.com/en-us/library/ty67wk28.aspx.
Null Check
To check for null using the ?: operator

ValueToCheck == null ? ValueIfNull : ValueIfNotNull;

Sample:

var value = ViewState["AValue"];
MyTextbox.Text = (value != null) ? value .ToString() : String.Empty;
Example - Minimum-Maximum range restriction
//Perform a Minimum/Maximum comparision to restrict a value to a range
//Set our minimum and maximum limit values
int Min = 0;
int Max = 100;
//Perform three comparisons. One with value below the minimum, one with Value above the maximum
//and one with Value between the minimum and maximum
int A = MinMax(-2, Min, Max);
int B = MinMax(102, Min, Max);
int C = MinMax(25, Min, Max);
//Display the results
Console.WriteLine("Value of A: {0}", A, B, C);
Console.WriteLine("Value of B: {1}", A, B, C);
Console.WriteLine("Value of C: {2}", A, B, C);

private static int MinMax(int Value, int MinVal, int MaxVal)
{
    //if Value is < MinVal, MinVal is returned.
    //if Value is >= MinVal, compare Value to MaxVal
    //  if Value is > MaxVal, MaxVal is returned
    //  if Value is <= MaxVal, Value is returned
    return Value < MinVal ? MinVal : (Value > MaxVal ? MaxVal : Value);
}

//Output
//Value of A: 0
//Value of B: 100
//Value of C: 25