13.07.2015 Views

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>C#</strong> LANGUAGE SPECIFICATIONThe result of != is false if both x and y are true or if both x and y are false. Otherwise, the result istrue. When the operands are of type bool, the != operator produces the same result as the ^ operator.14.9.5 Enumeration comparison operatorsEvery enumeration type implicitly provides the following predefined comparison operators:bool operator ==(E x, E y);bool operator !=(E x, E y);bool operator (E x, E y);bool operator =(E x, E y);The result of evaluating x op y, where x and y are expressions of an enumeration type E with an underlyingtype U, and op is one of the comparison operators, is exactly the same as evaluating ((U)x) op ((U)y). Inother words, the enumeration type comparison operators simply compare the underlying integral values ofthe two operands.14.9.6 Reference type equality operatorsThe predefined reference type equality operators are:bool operator ==(object x, object y);bool operator !=(object x, object y);The operators return the result of comparing the two references for equality or non-equality.Since the predefined reference type equality operators accept operands of type object, they apply to alltypes that do not declare applicable operator == and operator != members. Conversely, anyapplicable user-defined equality operators effectively hide the predefined reference type equality operators.The predefined reference type equality operators require the operands to be reference-type values or thevalue null; furthermore, they require that a standard implicit conversion (§13.3.1) exists from the type ofeither operand to the type of the other operand. Unless both of these conditions are true, a compile-time erroroccurs. [Note: Notable implications of these rules are:• It is a compile-time error to use the predefined reference type equality operators to compare tworeferences that are known to be different at compile-time. [Example: For example, if the compile-timetypes of the operands are two class types A and B, and if neither A nor B derives from the other, then itwould be impossible for the two operands to reference the same object. Thus, the operation is considereda compile-time error. end example]• The predefined reference type equality operators do not permit value type operands to be compared.Therefore, unless a struct type declares its own equality operators, it is not possible to compare values ofthat struct type.• The predefined reference type equality operators never cause boxing operations to occur for theiroperands. It would be meaningless to perform such boxing operations, since references to the newlyallocated boxed instances would necessarily differ from all other references.end note]For an operation of the form x == y or x != y, if any applicable operator == or operator != exists,the operator overload resolution (§14.2.4) rules will select that operator instead of the predefined referencetype equality operator. However, it is always possible to select the predefined reference type equalityoperator by explicitly casting one or both of the operands to type object. [Example: The example166

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!