Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C-sharp language specification.2004.pdf
Скачиваний:
14
Добавлен:
23.08.2013
Размер:
2.55 Mб
Скачать

Chapter 14 Expressions

114.9.7 String equality operators

2The predefined string equality operators are:

3bool operator ==(string x, string y);

4bool operator !=(string x, string y);

5Two string values are considered equal when one of the following is true:

6Both values are null.

7Both values are non-null references to string instances that have identical lengths and identical

8characters in each character position.

9The string equality operators compare string values rather than string references. When two separate string

10instances contain the exact same sequence of characters, the values of the strings are equal, but the

11references are different. [Note: As described in §14.9.6, the reference type equality operators can be used to

12compare string references instead of string values. end note]

1314.9.8 Delegate equality operators

14Every delegate type implicitly provides the following predefined comparison operators, where D is the

15delegate type :

16bool operator ==(D x, D y);

17bool operator !=(D x, D y);

18These operators are only considered by overload resolution (§14.2.4) when one of the actual operands is of

19type D.

20The following predefined operators are available for comparing delegates when the delegate type is not

21known at compile time:

22bool operator ==(System.Delegate x, System.Delegate y);

23bool operator !=(System.Delegate x, System.Delegate y);

24These latter operators are not considered applicable if the types of the operands are distinct delegate types.

25[Example:

26delegate void D();

27delegate void E();

28bool Compare1(D d, E e) {

29

return d == e;

// Error

30}

31bool Compare2(System.Delegate d, E e) {

32

return d == e;

// Ok

33}

34The comparison in Compare1 produces an error since the types of the operands are distinct delegate types.

35In this case, none of the delegate equality operators or reference equality operators are applicable since a

36non-null instance of D could never be equal to an instance of E. end example]

37Two delegate instances are considered equal as follows:

38If either of the delegate instances is null, they are equal if and only if both are null.

39If the delegate instances have different actual types, they are not equal.

40If either of the delegate instances has an invocation list (§22.1) containing one entry, they are equal if

41and only if the other also has an invocation list containing one entry and the entries are equal (as defined

42below).

43If both of the delegate instances has an invocation list containing two or more entries, those instances are

44equal if and only if their invocation lists are the same length, and each entry in one’s invocation list is

45equal (as defined below) to the corresponding entry, in order, in the other’s invocation list.

46The following rules govern the equality of invocation list entries:

203

C# LANGUAGE SPECIFICATION

1If two invocation list entries both refer to the same static method then the entries are equal.

2If two invocation list entries both refer to the same non-static method on the same target object (as

3defined by the reference equality operators) then the entries are equal.

4Invocation list entries produced from evaluation of semantically identical anonymous-method-

5expressions with the same (possibly empty) set of captured outer variable instances are permitted (but

6not required) to be equal.

7Invocation list entries produced from evaluation of semantically different anonymous-method-

8expressions or having different sets of captured outer variable instances are never equal.

9An invocation list entry produced from evaluation of an anonymous-method-expression is never equal to

10an invocation list entry produced from a delegate-creation-expression on a method group.

1114.9.9 The is operator

12The is operator is used to dynamically check if the run-time type of an object is compatible with a given

13type. The result of the operation e is T, where e is an expression and T is a type, is a boolean value

14indicating whether e can successfully be converted to type T by a reference conversion, a boxing conversion,

15or an unboxing conversion. The operation is evaluated as follows:

16If the compile-time type of e is the same as T, or if an implicit reference conversion (§13.1.4) or boxing

17conversion (§13.1.5) exists from the compile-time type of e to T:

18o If e is of a reference type, the result of the operation is equivalent to evaluating e != null.

19o If e is of a value type, the result of the operation is true.

20Otherwise, if an explicit reference conversion (§13.2.3) or unboxing conversion (§13.2.4) exists from

21the compile-time type of e to T, a dynamic type check is performed:

22o If the value of e is null, the result is false.

23o Otherwise, let R be the run-time type of the instance referenced by e. If R and T are the same type, if

24R is a reference type and an implicit reference conversion from R to T exists, or if R is a value type

25and T is an interface type that is implemented by R, the result is true.

26o Otherwise, the result is false.

27Otherwise, if the compile-time type of e or T is an open type (§26.5.2), a dynamic type check is

28performed.

29Otherwise, no reference or boxing conversion of e to type T is possible, and the result of the operation is

30false.

31The is operator only considers reference conversions, boxing conversions, and unboxing conversions. Other

32conversions, such as user defined conversions, are not considered by the is operator.

3314.9.10 The as operator

34The as operator is used to explicitly convert a value to a given reference type using a reference conversion

35or a boxing conversion. Unlike a cast expression (§14.6.6), the as operator never throws an exception.

36Instead, if the indicated conversion is not possible, the resulting value is null.

37In an operation of the form e as T, e shall be an expression and T shall be a reference type or a type

38parameter that is known to be a reference type (§26.7). The type of the result is T, and the result is always

39classified as a value. The operation is evaluated as follows:

40If the compile-time type of e is the same as T, the result is simply the value of e.

41Otherwise, if an implicit reference conversion (§13.1.4) or boxing conversion (§13.1.5) exists from the

42compile-time type of e to T, this conversion is performed and becomes the result of the operation.

204

Соседние файлы в предмете Электротехника