X hits on this document

967 views

0 shares

0 downloads

0 comments

166 / 396

Chapter ‎18   Unsafe code

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

Two delegate instances are considered equal as follows:

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

If either of the delegate instances has an invocation list (§‎15.1) containing one entry, they are equal if and only if the other also has an invocation list containing one entry, and either:

both refer to the same static method, or

both refer to the same non-static method on the same target object.

If either of the delegate instances has an invocation list containing two or more entries, those instances are equal if and only if their invocation lists are the same length, and each entry in one’s invocation list is equal to the corresponding entry, in order, in the other’s invocation list.

Note that delegates of different types can be considered equal by the above definition, as long as they have the same return type and parameter types.

7.9.9 The is operator

The is operator is used to dynamically check if the run-time type of an object is compatible with a given type. The result of the operation e is T, where e is an expression and T is a type, is a boolean value indicating whether e can successfully be converted to type T by a reference conversion, a boxing conversion, or an unboxing conversion. The operation is evaluated as follows:

If the compile-time type of e is the same as T, or if an implicit reference conversion (§‎6.1.4) or boxing conversion (§‎6.1.5) exists from the compile-time type of e to T:

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

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

Otherwise, if an explicit reference conversion (§‎6.2.3) or unboxing conversion (§‎6.2.4) exists from the compile-time type of e to T, a dynamic type check is performed:

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

Otherwise, let R be the run-time type of the instance referenced by e. If R and T are the same type, if R is a reference type and an implicit reference conversion from R to T exists, or if R is a value type and T is an interface type that is implemented by R, the result is true.

Otherwise, the result is false.

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

Note that the is operator only considers reference conversions, boxing conversions, and unboxing conversions. Other conversions, such as user defined conversions, are not considered by the is operator.

7.9.10 The as operator

The as operator is used to explicitly convert a value to a given reference type using a reference conversion or a boxing conversion. Unlike a cast expression (§‎7.6.6), the as operator never throws an exception. Instead, if the indicated conversion is not possible, the resulting value is null.

In an operation of the form e as T, e must be an expression and T must be a reference type. The type of the result is T, and the result is always classified as a value. The operation is evaluated as follows:

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

Copyright Microsoft Corporation 1999-2003. All Rights Reserved.153

Document info
Document views967
Page views967
Page last viewedThu Dec 08 16:48:01 UTC 2016
Pages396
Paragraphs9401
Words133190

Comments