Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
16
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 8 EXPRESSIONS AND OPERATORS

The Conditional Operator

The conditional operator is a powerful and succinct way of returning one of two values, based on the result of a condition. Table 8-15 shows the operator.

The conditional operator is ternary.

Table 8-15. The Conditional Operator

Operator

Name

Description

? :

Conditional operator

Evaluates an expression and returns one of two values,

 

 

depending on whether the expression returns true or false

 

 

 

The syntax for the conditional operator is shown following. It has a test expression and two result expressions.

Condition must return a value of type bool.

If Condition evaluates to true, then Expression1 is evaluated and returned. Otherwise, Expression2 is evaluated and returned.

Condition ? Expression1 : Expression2

The conditional operator can be compared with the if...else construct. For example, the following if...else construct checks a condition, and if the condition is true, it assigns 5 to variable intVar. Otherwise, it assigns it the value 10.

if ( x < y )

// if...else

intVar = 5;

 

else

 

intVar = 10;

 

The conditional operator can perform the same operation in a less verbose form, as shown in the following statement:

intVar = x < y ? 5 : 10;

// Conditional operator

Placing the condition and each return expression on separate lines, as in the following code, makes the intent very easy to understand.

intVar = x < y

?5

: 10 ;

227

CHAPTER 8 EXPRESSIONS AND OPERATORS

Figure 8-9 compares the two forms shown in the example.

Figure 8-9. The conditional operator versus if...else

For example, the following code uses the conditional operator three times—once in each of the WriteLine statements. In the first instance, it returns either the value of x or the value of y. In the second two instances, it returns either the empty string or the string “ not”.

int x = 10, y = 9;

 

int highVal = x > y

// Condition

? x

// Expression 1

: y;

// Expression 2

Console.WriteLine("highVal: {0}\n" , highVal);

 

Console.WriteLine("x is{0} greater than y" ,

 

x > y

// Condition

? ""

// Expression 1

: " not" );

// Expression 2

y = 11;

 

Console.WriteLine("x is{0} greater than y" ,

 

x > y

// Condition

? ""

// Expression 1

: " not" );

// Expression 2

This code produces the following output:

 

highVal: 10

 

x is greater than y

 

x is not greater than y

 

Note The if...else statement is a flow-of-control statement. It should be used for doing one or the other of two actions. The conditional operator returns an expression. It should be used for returning one or the other of two values.

228

CHAPTER 8 EXPRESSIONS AND OPERATORS

Unary Arithmetic Operators

The unary operators set the sign of a numeric value. They are listed in Table 8-16.

The unary positive operator simply returns the value of the operand.

The unary negative operator returns the value of the operand subtracted from 0.

Table 8-16. The Unary Operators

Operator

Name

Description

+

Positive sign

Returns the numeric value of the operand

-

Negative sign

Returns the numeric value of the operand subtracted from 0

 

 

 

For example, the following code shows the use and results of the operators:

int x = +10; int y = -x; int z = -y;

//x = 10

//y = -10

//z = 10

229

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]