Scrigroup - Documente si articole

     

HomeDocumenteUploadResurseAlte limbi doc
AccessAdobe photoshopAlgoritmiAutocadBaze de dateCC sharp
CalculatoareCorel drawDot netExcelFox proFrontpageHardware
HtmlInternetJavaLinuxMatlabMs dosPascal
PhpPower pointRetele calculatoareSqlTutorialsWebdesignWindows
WordXml


Unary expressions

C sharp



+ Font mai mare | - Font mai mic



Unary expressions

Issue



We need to write this section.

unary-expression:
primary-expression
+ unary-expression
- unary-expression
! unary-expression
~ unary-expression
* unary-expression
pre-increment-expression
pre-decrement-expression
cast-expression

Unary plus operator

For an operation of the form +x, unary operator overload resolution (7.2.3) is applied to select a specific operator implementation. The operand is converted to the parameter type of the selected operator, and the type of the result is the return type of the operator. The predefined unary plus operators are:

int operator +(int x);
uint operator +(uint x);
long operator +(long x);
ulong operator +(ulong x);
float operator +(float x);
double operator +(double x);
decimal operator +(decimal x);

For each of these operators, the result is simply the value of the operand.

Unary minus operator

For an operation of the form -x, unary operator overload resolution (7.2.3) is applied to select a specific operator implementation. The operand is converted to the parameter type of the selected operator, and the type of the result is the return type of the operator. The predefined negation operators are:

Integer negation:

int operator -(int x);
long operator -(long x);

The result is computed by subtracting x from zero. In a checked context, if the value of x is the maximum negative int or long, a System.OverflowException is thrown. In an unchecked context, if the value of x is the maximum negative int or long, the result is that same value and the overflow is not reported.

If the operand of the negation operator is of type uint, it is converted to type long, and the type of the result is long. An exception is the rule that permits the int value −2147483648 (−231) to be written as a decimal integer literal (2.4.4.2).

If the operand of the negation operator is of type ulong, an error occurs. An exception is the rule that permits the long value −9223372036854775808 (−263) to be written as decimal integer literal (2.4.4.2).

Floating-point negation:

float operator -(float x);
double operator -(double x);

The result is the value of x with its sign inverted. If x is NaN, the result is also NaN.

Decimal negation:

decimal operator -(decimal x);

The result is computed by subtracting x from zero.

Logical negation operator

For an operation of the form !x, unary operator overload resolution (7.2.3) is applied to select a specific operator implementation. The operand is converted to the parameter type of the selected operator, and the type of the result is the return type of the operator. Only one predefined logical negation operator exists:

bool operator !(bool x);

This operator computes the logical negation of the operand: If the operand is true, the result is false. If the operand is false, the result is true.

Bitwise complement operator

For an operation of the form ~x, unary operator overload resolution (7.2.3) is applied to select a specific operator implementation. The operand is converted to the parameter type of the selected operator, and the type of the result is the return type of the operator. The predefined bitwise complement operators are:

int operator ~(int x);
uint operator ~(uint x);
long operator ~(long x);
ulong operator ~(ulong x);

For each of these operators, the result of the operation is the bitwise complement of x.

Every enumeration type E implicitly provides the following bitwise complement operator:

E operator ~(E x);

The result of evaluating ~x, where x is an expression of an enumeration type E with an underlying type U, is exactly the same as evaluating (E)(~(U)x).

Indirection operator

Address operator

Prefix increment and decrement operators

pre-increment-expression:
++ unary-expression

pre-decrement-expression:
-- unary-expression

The operand of a prefix increment or decrement operation must be an expression classified as a variable, a property access, or an indexer access. The result of the operation is a value of the same type as the operand.

If the operand of a prefix increment or decrement operation is a property or indexer access, the property or indexer must have both a get and a set accessor. If this is not the case, a compile-time error occurs.

Unary operator overload resolution (7.2.3) is applied to select a specific operator implementation. Predefined ++ and -- operators exist for the following types: sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, and any enum type. The predefined ++ operators return the value produced by adding 1 to the operand, and the predefined -- operators return the value produced by subtracting 1 from the operand.

The run-time processing of a prefix increment or decrement operation of the form ++x or --x consists of the following steps:

If x is classified as a variable:

x is evaluated to produce the variable.

The selected operator is invoked with the value of x as its argument.

The value returned by the operator is stored in the location given by the evaluation of x.

The value returned by the operator becomes the result of the operation.

If x is classified as a property or indexer access:

The instance expression (if x is not static) and the argument list (if x is an indexer access) associated with x are evaluated, and the results are used in the subsequent get and set accessor invocations.

The get accessor of x is invoked.

The selected operator is invoked with the value returned by the get accessor as its argument.

The set accessor of x is invoked with the value returned by the operator as its value argument.

The value returned by the operator becomes the result of the operation.

The ++ and -- operators also support postfix notation, as described in 7.5.9. The result of x++ or x-- is the value of x before the operation, whereas the result of ++x or --x is the value of x after the operation. In either case, x itself has the same value after the operation.

An operator ++ or operator -- implementation can be invoked using either postfix or prefix notation. It is not possible to have separate operator implementations for the two notations.

Cast expressions

A cast-expression is used to explicitly convert an expression to a given type.

cast-expression:
( type ) unary-expression

A cast-expression of the form (T)E, where T is a type and E is a unary-expression, performs an explicit conversion (6.2) of the value of E to type T. If no explicit conversion exists from the type of E to T, an error occurs. Otherwise, the result is the value produced by the explicit conversion. The result is always classified as a value, even if E denotes a variable.

The grammar for a cast-expression leads to certain syntactic ambiguities. For example, the expression (x)-y could either be interpreted as a cast-expression (a cast of -y to type x) or as an additive-expression combined with a parenthesized-expression (which computes the value x - y).

To resolve cast-expression ambiguities, the following rule exists: A sequence of one or more tokens (2.4) enclosed in parentheses is considered the start of a cast-expression only if at least one of the following are true:

The sequence of tokens is correct grammar for a type, but not for an expression.

The sequence of tokens is correct grammar for a type, and the token immediately following the closing parentheses is the token "~", the token "!", the token "(", an identifier (2.4.1), a literal (2.4.4), or any keyword (2.4.3) except as and is.

The above rules mean that only if the construct is unambiguously a cast-expression is it considered a cast-expression.

The term "correct grammar" above means only that the sequence of tokens must conform to the particular grammatical production. It specifically does not consider the actual meaning of any constituent identifiers. For example, if x and y are identifiers, then x.y is correct grammar for a type, even if x.y doesn't actually denote a type.

From the disambiguation rules it follows that, if x and y are identifiers, (x)y, (x)(y), and (x)(-y) are cast-expressions, but (x)-y is not, even if x identifies a type. However, if x is a keyword that identifies a predefined type (such as int), then all four forms are cast-expressions (because such a keyword could not possibly be an expression by itself).



Politica de confidentialitate | Termeni si conditii de utilizare



DISTRIBUIE DOCUMENTUL

Comentarii


Vizualizari: 1001
Importanta: rank

Comenteaza documentul:

Te rugam sa te autentifici sau sa iti faci cont pentru a putea comenta

Creaza cont nou

Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved