Changes

Jump to: navigation, search

C Sharp Operators and Expressions

782 bytes added, 21:19, 14 January 2008
The Ternary Operator
== The Ternary Operator ==
 
C# uses something called a ''ternary operator'' to provide a shortcut way of making decisions. The syntax of the ternary operator is as follows:
 
[condition] ? [true expression] : [false expression]
 
The way this works is that [condition] is replaced with an expression that will return either ''true'' or ''false''. If the result is true then the expression that replaces the [true expression] is evaluated. Conversely, if the result was ''false'' then the [false expression] is evaluated. Let's see this in action:
 
<pre>
int x = 10;
int y = 20;
 
System.Console.WriteLine( x > y ? x : y );
</pre>
 
The above code example will evaluate whether x is greater than y. Clearly this will evaluate to false resulting in y being returned to the WriteLine method for display to the user.

Navigation menu