Skip to content

Update expressions #1211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 7, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions docs/csharp/tour-of-csharp/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ description: expressions, operands, and operators are building blocks of the C#
keywords: .NET, csharp, expression, operator, operand
author: BillWagner
manager: wpickett
ms.date: 2016/08/10
ms.author: wiwagn
ms.date: 2016/11/06
ms.topic: article
ms.prod: visual-studio-dev-14
ms.technology: devlang-csharp
Expand All @@ -14,18 +15,18 @@ ms.assetid: 20d5eb10-7381-47b9-ad90-f1cc895aa27e

# Expressions

***Expressions*** are constructed from ***operands*** and ***operators***. The operators of an expression indicate which operations to apply to the operands. Examples of operators include `+`, `-`, `*`, `/`, and `new`. Examples of operands include literals, fields, local variables, and expressions.
*Expressions* are constructed from *operands* and *operators*. The operators of an expression indicate which operations to apply to the operands. Examples of operators include `+`, `-`, `*`, `/`, and `new`. Examples of operands include literals, fields, local variables, and expressions.

When an expression contains multiple operators, the ***precedence*** of the operators controls the order in which the individual operators are evaluated. For example, the expression `x + y * z` is evaluated as `x + (y * z)` because the `*` operator has higher precedence than the `+` operator.
When an expression contains multiple operators, the *precedence* of the operators controls the order in which the individual operators are evaluated. For example, the expression `x + y * z` is evaluated as `x + (y * z)` because the `*` operator has higher precedence than the `+` operator.

When an operand occurs between two operators with the same precedence, the ***associativity*** of the operators controls the order in which the operations are performed:
When an operand occurs between two operators with the same precedence, the *associativity* of the operators controls the order in which the operations are performed:

* Except for the assignment operators, all binary operators are ***left-associative***, meaning that operations are performed from left to right. For example, `x + y + z` is evaluated as `(x + y) + z`.
* The assignment operators and the conditional operator (`?:`) are ***right-associative***, meaning that operations are performed from right to left. For example, `x = y = z` is evaluated as `x = (y = z)`.
* Except for the assignment operators, all binary operators are *left-associative*, meaning that operations are performed from left to right. For example, `x + y + z` is evaluated as `(x + y) + z`.
* The assignment operators and the conditional operator (`?:`) are *right-associative*, meaning that operations are performed from right to left. For example, `x = y = z` is evaluated as `x = (y = z)`.

Precedence and associativity can be controlled using parentheses. For example, `x + y * z` first multiplies `y` by `z` and then adds the result to `x`, but `(x + y) * z` first adds `x` and `y` and then multiplies the result by `z`.

Most operators can be ***overloaded***. Operator overloading permits user-defined operator implementations to be specified for operations where one or both of the operands are of a user-defined class or struct type.
Most operators can be *overloaded*. Operator overloading permits user-defined operator implementations to be specified for operations where one or both of the operands are of a user-defined class or struct type.

The following summarizes C#’s operators, listing the operator categories in order of precedence from highest to lowest. Operators in the same category have equal precedence. Under each category is a list of expressions in that category along with the description of that expression type.

Expand All @@ -39,7 +40,7 @@ The following summarizes C#’s operators, listing the operator categories in or
- `new T(...){...}`: Object creation with initializer
- `new {...}`: Anonymous object initializer
- `new T[...]`: Array creation
- `typeof(T)`: Obtain System.Type object for `T`
- `typeof(T)`: Obtain @System.Type object for `T`
- `checked(x)`: Evaluate expression in checked context
- `unchecked(x)`: Evaluate expression in unchecked context
- `default(T)`: Obtain default value of type `T`
Expand Down Expand Up @@ -78,13 +79,13 @@ The following summarizes C#’s operators, listing the operator categories in or
* Logical XOR
- `x ^ y`: Integer bitwise XOR, boolean logical XOR
* Logical OR
`x | y`: Integer bitwise OR, boolean logical OR
- `x | y`: Integer bitwise OR, boolean logical OR
* Conditional AND
- `x && y`: Evaluates `y` only if `x` is not `false`
* Conditional OR
- `x || y`: Evaluates `y` only if `x` is not `true`
* Null coalescing
- `X ?? y`: Evaluates to `y` if `x` is null, to `x` otherwise
- `x ?? y`: Evaluates to `y` if `x` is null, to `x` otherwise
* Conditional
- `x ? y : z`: Evaluates `y` if `x` is `true`, `z` if `x` is `false`
* Assignment or anonymous function
Expand Down