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 SPECIFICATION14.1.1 Values of expressionsMost of the constructs that involve an expression ultimately require the expression to denote a value. In suchcases, if the actual expression denotes a namespace, a type, a method group, or nothing, a compile-time erroroccurs. However, if the expression denotes a property access, an indexer access, or a variable, the value ofthe property, indexer, or variable is implicitly substituted:• The value of a variable is simply the value currently stored in the storage location identified by thevariable. A variable must be considered definitely assigned (§12.3) before its value can be obtained, orotherwise a compile-time error occurs.• The value of a property access expression is obtained by invoking the get-accessor of the property. If theproperty has no get-accessor, a compile-time error occurs. Otherwise, a function member invocation(§14.4.3) is performed, and the result of the invocation becomes the value of the property accessexpression.• The value of an indexer access expression is obtained by invoking the get-accessor of the indexer. If theindexer has no get-accessor, a compile-time error occurs. Otherwise, a function member invocation(§14.4.3) is performed with the argument list associated with the indexer access expression, and theresult of the invocation becomes the value of the indexer access expression.14.2 OperatorsExpressions are constructed from operands and operators. The operators of an expression indicate whichoperations to apply to the operands. [Example: Examples of operators include +, -, *, /, and new. Examplesof operands include literals, fields, local variables, and expressions. end example]There are three kinds of operators:• Unary operators. The unary operators take one operand and use either prefix notation (such as –x) orpostfix notation (such as x++).• Binary operators. The binary operators take two operands and all use infix notation (such as x + y).• Ternary operator. Only one ternary operator, ?:, exists; it takes three operands and uses infix notation(c ? x : y).The order of evaluation of operators in an expression is determined by the precedence and associativity ofthe operators (§14.2.1).The order in which operands in an expression are evaluated, is left to right. [Example: For example, inF(i) + G(i++) * H(i), method F is called using the old value of i, then method G is called with the oldvalue of i, and, finally, method H is called with the new value of i. This is separate from and unrelated tooperator precedence. end example] Certain operators can be overloaded. Operator overloading permits userdefinedoperator implementations to be specified for operations where one or both of the operands are of auser-defined class or struct type (§14.2.2).14.2.1 Operator precedence and associativityWhen an expression contains multiple operators, the precedence of the operators controls the order in whichthe individual operators are evaluated. [Note: For example, the expression x + y * z is evaluated asx + (y * z) because the * operator has higher precedence than the binary + operator. end note] Theprecedence of an operator is established by the definition of its associated grammar production. [Note: Forexample, an additive-expression consists of a sequence of multiplicative-expressions separated by + or -operators, thus giving the + and - operators lower precedence than the *, /, and % operators. end note]The following table summarizes all operators in order of precedence from highest to lowest:124

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

Saved successfully!

Ooh no, something went wrong!