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

CHAPTER 8 EXPRESSIONS AND OPERATORS

Relational and Equality Comparison Operators

The relational and equality comparison operators are binary operators that compare their operands and return a value of type bool. Table 8-8 lists these operators.

The relational and equality operators are binary and left-associative.

Table 8-8. The Relational and Equality Comparison Operators

Operator

Name

Description

<

Less than

true if first operand is less than second operand; false

 

 

otherwise

>

Greater than

true if first operand is greater than second operand; false

 

 

otherwise

<=

Less than or equal to

true if first operand is less than or equal to second operand;

 

 

false otherwise

>=

Greater than or equal to

true if first operand is greater than or equal to second

 

 

operand; false otherwise

==

Equal to

true if first operand is equal to second operand; false

 

 

otherwise

!=

Not equal to

true if first operand is not equal to second operand; false

 

 

otherwise

 

 

 

A binary expression with a relational or equality operator returns a value of type bool.

Note Unlike C and C++, numbers in C# do not have a Boolean interpretation.

int x = 5;

 

 

if(

x

)

//

Wrong. x is of type int, not type boolean.

 

...

 

 

if(

x

== 5 )

//

Fine, since expression returns a value of type boolean

 

...

 

 

When printed, the Boolean values true and false are represented by the string output values True and False.

int x = 5, y = 4;

Console.WriteLine("x == x is {0}" , x == x); Console.WriteLine("x == y is {0}" , x == y);

214

CHAPTER 8 EXPRESSIONS AND OPERATORS

The output of this code is the following:

x == x is True x == y is False

Comparison and Equality Operations

When comparing most reference types for equality, only the references are compared.

If the references are equal—that is, if they point to the same object in memory—the equality comparison is true; otherwise, it is false, even if the two separate objects in memory are exactly equivalent in every other respect.

This is called a shallow comparison.

Figure 8-4 illustrates the comparison of reference types.

On the left of the figure, the references of both a and b are the same, so a comparison would return true.

On the right of the figure, the references are not the same, so even if the contents of the two AClass objects were exactly the same, the comparison would return false.

Figure 8-4. Comparing reference types for equality

215

CHAPTER 8 EXPRESSIONS AND OPERATORS

Objects of type string are also reference types but are compared differently. When strings are compared for equality, they are compared in length and case-sensitive content.

If two strings have the same length and the same case-sensitive content, the equality comparison returns true, even if they occupy different areas of memory.

This is called a deep comparison.

Delegates, which are covered in Chapter 15, are also reference types and also use deep comparison. When delegates are compared for equality, the comparison returns true if both delegates are null or if both have the same number of members in their invocation lists and the invocation lists match.

When comparing numeric expressions, the types and values are compared. When comparing enum types, the comparisons are done on the underlying values of the operands. Enums are covered in Chapter 13.

216

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