CATEGORII DOCUMENTE 
loading...



DOCUMENTE SIMILARE 



The ==, !=, <, >, <=, >=, is and as operators are called the relational and type testing operators.


relationalexpression:
shiftexpression
relationalexpression < shiftexpression
relationalexpression > shiftexpression
relationalexpression <= shiftexpression
relationalexpression >= shiftexpression
relationalexpression is type
relationalexpression as type
equalityexpression:
relationalexpression
equalityexpression == relationalexpression
equalityexpression != relationalexpression
The is operator is described in §7.9.9 and the as operator is described in §7.9.10.
The ==, !=, <, >, <= and >= operators are comparison operators. For an operation of the form x op y, where op is a comparison operator, overload resolution (§7.2.4) is applied to select a specific operator implementation. The operands are converted to the parameter types of the selected operator, and the type of the result is the return type of the operator.
The predefined comparison operators are described in the following sections. All predefined comparison operators return a result of type bool, as described in the following table.
Operation 
Result 
x == y 
true if x is equal to y, false otherwise 
x != y 
true if x is not equal to y, false otherwise 
x < y 
true if x is less than y, false otherwise 
x > y 
true if x is greater than y, false otherwise 
x <= y 
true if x is less than or equal to y, false otherwise 
x >= y 
true if x is greater than or equal to y, false otherwise 
The predefined integer comparison operators are:
bool
operator ==(int
x, int y);
bool operator ==(uint
x, uint y);
bool operator ==(long
x, long y);
bool operator ==(ulong
x, ulong y);
bool
operator !=(int
x, int y);
bool operator !=(uint
x, uint y);
bool operator !=(long
x, long y);
bool operator !=(ulong
x, ulong y);
bool
operator <(int
x, int y);
bool operator <(uint
x, uint y);
bool operator <(long
x, long y);
bool operator <(ulong
x, ulong y);
bool
operator >(int
x, int y);
bool operator >(uint
x, uint y);
bool operator >(long
x, long y);
bool operator >(ulong
x, ulong y);
bool
operator <=(int
x, int y);
bool operator <=(uint
x, uint y);
bool operator <=(long
x, long y);
bool operator <=(ulong
x, ulong y);
bool
operator >=(int
x, int y);
bool operator >=(uint
x, uint y);
bool operator >=(long
x, long y);
bool operator >=(ulong
x, ulong y);
Each of these operators compares the numeric values of the two integer operands and returns a bool value that indicates whether the particular relation is true or false.
The predefined floatingpoint comparison operators are:
bool
operator ==(float
x, float y);
bool operator ==(double
x, double y);
bool
operator !=(float
x, float y);
bool operator !=(double
x, double y);
bool
operator <(float
x, float y);
bool operator <(double
x, double y);
bool
operator >(float
x, float y);
bool operator >(double
x, double y);
bool
operator <=(float
x, float y);
bool operator <=(double
x, double y);
bool
operator >=(float
x, float y);
bool operator >=(double
x, double y);
The operators compare the operands according to the rules of the IEEE 754 standard:
If
either operand is
When
neither operand is
–∞ < –max < < –min < –0.0 == +0.0 < +min < < +max < +∞
where min and max are the smallest and largest positive finite values that can be represented in the given floatingpoint format. Notable effects of this ordering are:
Negative and positive zero are considered equal.
A negative infinity is considered less than all other values, but equal to another negative infinity.
A positive infinity is considered greater than all other values, but equal to another positive infinity.
The predefined decimal comparison operators are:
bool operator ==(decimal x, decimal y);
bool operator !=(decimal x, decimal y);
bool operator <(decimal x, decimal y);
bool operator >(decimal x, decimal y);
bool operator <=(decimal x, decimal y);
bool operator >=(decimal x, decimal y);
Each of these operators compare the numeric values of the two decimal operands and return a bool value that indicates whether the particular relation is true or false.
The predefined boolean equality operators are:
bool operator ==(bool x, bool y);
bool operator !=(bool x, bool y);
The result of == is true if both x and y are true or if both x and y are false. Otherwise, the result is false.
The result of != is false if both x and y are true or if both x and y are false. Otherwise, the result is true. When the operands are of type bool, the != operator produces the same result as the ^ operator.
Every 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);
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 underlying type U, and op is one of the comparison operators, is exactly the same as evaluating ((U)x) op ((U)y). In other words, the enumeration type comparison operators simply compare the underlying integral values of the two operands.
The 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 nonequality.
Since the predefined reference type equality operators accept operands of type object, they apply to all types that do not declare applicable operator == and operator != members. Conversely, any applicable userdefined equality operators effectively hide the predefined reference type equality operators.
The predefined reference type equality operators require the operands to be referencetype values or the value null. Furthermore, they require that an implicit conversion exists from the type of either operand to the type of the other operand. Unless both of these conditions are true, a compiletime error occurs. Notable implications of these rules are:
It is an error to use the predefined reference type equality operators to compare two references that are known to be different at compiletime. For example, if the compiletime types of the operands are two class types A and B, and if neither A nor B derives from the other, then it would be impossible for the two operands to reference the same object. Thus, the operation is considered a compiletime error.
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 of that struct type.
The predefined reference type equality operators never cause boxing operations to occur for their operands. It would be meaningless to perform such boxing operations, since references to the newly allocated boxed instances would necessarily differ from all other references.
For an operation of the form x == y or x != y, if any applicable operator == or operator != exists, the operator overload resolution (§7.2.4) rules will select that operator instead of the predefined reference type equality operator. However, it is always possible to select the predefined reference type equality operator by explicitly casting one or both of the operands to type object. The example
class
Test
}
produces the output
True
False
False
False
The s and t variables refer to two distinct string instances containing the same characters. The first comparison outputs True because the predefined string equality operator (§7.9.7) is selected when both operands are of type string. The remaining comparisons all output False because the predefined reference type equality operator is selected when one or both of the operands are of type object.
Note that the above technique is not meaningful for value types. The example
class
Test
}
outputs False because the casts create references to two separate instances of boxed int values.
The predefined string equality operators are:
bool operator ==(string x, string y);
bool operator !=(string x, string y);
Two string values are considered equal when one of the following is true:
Both values are null.
Both values are nonnull references to string instances that have identical lengths and identical characters in each character position.
Every delegate type implicitly provides the following predefined comparison operators:
bool operator ==(System.Delegate x, System.Delegate y);
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 nonstatic 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.
The is operator is used to dynamically check if the runtime 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 compiletime 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 compiletime 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 compiletime 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 runtime 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.
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.8), 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 compiletime type of e is the same as T, the result is simply the value of e.
Otherwise, if an implicit reference conversion (§6.1.4) or boxing conversion (§6.1.5) exists from the compiletime type of e to T, this conversion is performed and becomes the result of the operation.
Otherwise, if an explicit reference conversion (§6.2.3) exists from the compiletime type of e to T, a dynamic type check is performed:
If the value of e is null, the result is the value null with the compiletime type T.
Otherwise, let R be the runtime 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 the reference given by e with the compiletime type T.
Otherwise, the result is the value null with the compiletime type T.
Otherwise, the indicated conversion is never possible, and a compiletime error occurs.
Note that the as operator only performs reference conversions and boxing conversions. Other conversions, such as user defined conversions, are not possible with the as operator and should instead be performed using cast expressions.
loading...

Vizualizari: 389
Importanta:
Termeni si conditii de utilizare  Contact
© SCRIGROUP 2020 . All rights reserved
Distribuie URL
Adauga cod HTML in site