Skip to content
Draft
Show file tree
Hide file tree
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
10 changes: 9 additions & 1 deletion .openpublishing.redirection.csharp.json
Original file line number Diff line number Diff line change
Expand Up @@ -5068,6 +5068,10 @@
"source_path_from_root": "/docs/csharp/programming-guide/statements-expressions-operators/default-value-expressions.md",
"redirect_url": "/dotnet/csharp/language-reference/operators/default"
},
{
"source_path_from_root": "/docs/csharp/programming-guide/statements-expressions-operators/expression-bodied-members.md",
"redirect_url": "/dotnet/csharp/language-reference/operators/lambda-operator#expression-body-definition"
},
{
"source_path_from_root": "/docs/csharp/programming-guide/statements-expressions-operators/expressions.md",
"redirect_url": "/dotnet/csharp/language-reference/operators/index"
Expand All @@ -5090,7 +5094,7 @@
},
{
"source_path_from_root": "/docs/csharp/programming-guide/statements-expressions-operators/index.md",
"redirect_url": "/dotnet/csharp/programming-guide/statements-expressions-operators/statements"
"redirect_url": "/dotnet/csharp/fundamentals/statements"
},
{
"source_path_from_root": "/docs/csharp/programming-guide/statements-expressions-operators/lambda-expressions.md",
Expand All @@ -5104,6 +5108,10 @@
"source_path_from_root": "/docs/csharp/programming-guide/statements-expressions-operators/overloadable-operators.md",
"redirect_url": "/dotnet/csharp/language-reference/operators/operator-overloading#overloadable-operators"
},
{
"source_path_from_root": "/docs/csharp/programming-guide/statements-expressions-operators/statements.md",
"redirect_url": "/dotnet/csharp/fundamentals/statements"
},
{
"source_path_from_root": "/docs/csharp/programming-guide/statements-expressions-operators/using-conversion-operators.md",
"redirect_url": "/dotnet/csharp/language-reference/operators/user-defined-conversion-operators"
Expand Down
4 changes: 4 additions & 0 deletions docs/csharp/fundamentals/null-safety/null-operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ Use `!` sparingly, and only when you have information the compiler doesn't. Exam

## See also

<!-- Remove this HTML comment after dotnet/docs#55469 is merged and this branch is rebased.
- [Expressions overview](../expressions/index.md)
-->

- [Null safety overview](index.md)
- [Nullable value types](nullable-value-types.md)
- [Nullable reference types](nullable-reference-types.md)
Expand Down
2 changes: 1 addition & 1 deletion docs/csharp/fundamentals/program-structure/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ Statements often contain expressions, and expressions can nest inside other expr
var maxResult = Math.Max(a, b) + Math.Max(c, d);
```

For detailed information about statements, see [Statements](../../programming-guide/statements-expressions-operators/statements.md). For information about expression-bodied members, see [Expression-bodied members](../../programming-guide/statements-expressions-operators/expression-bodied-members.md).
For detailed information about statements, see [Statements](../statements/index.md). For information about expression-bodied members, see [Expression body definitions](../../language-reference/operators/lambda-operator.md#expression-body-definition).

## Related content

Expand Down
105 changes: 105 additions & 0 deletions docs/csharp/fundamentals/statements/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
---
title: "C# statements"
description: Learn how C# statements declare variables, perform actions, group code into blocks, and control the flow of execution.
ms.date: 08/20/2026
ms.topic: concept-article
ai-usage: ai-assisted
---

# C# statements

> [!TIP]
> This article is part of the **Fundamentals** section for developers who already know at least one programming language and are learning C#. If you're new to programming, start with the [Get started](../../tour-of-csharp/tutorials/index.md) tutorials first. For complete statement syntax, see [Statements](~/_csharpstandard/standard/statements.md) in the C# language specification.
>
> **Coming from another language?** Declarations, conditions, loops, and returns might be familiar. C# uses its own syntax and classification for these features, which this article introduces.

A *statement* is a complete command: "do this." Together, the statements in a program form a recipe that the program follows from start to finish. Most statements run in sequence. Branches choose which steps to run, and loops repeat steps.

This example declares a quantity, displays it, and then uses an `if` statement to decide whether to restock:

:::code language="csharp" source="./snippets/statements-overview/Program.cs" id="StatementRecipe":::

Read the example as complete commands before looking at their parts. The declaration, the first call to `Console.WriteLine`, the entire `if` construct, and the final call to `Console.WriteLine` are statements. The block inside the `if` statement contains two more statements.

## Statements often contain expressions

Statements often contain *expressions*, which are pieces of code that produce values. In the preceding example, the whole `if` construct is a statement. Its condition, `quantity < 10`, is an expression that produces either `true` or `false`.

A *declaration statement* introduces a local variable or constant. An initializer expression can provide its first value:

```csharp
int quantity = 5;
```

The complete line is a declaration statement. The initializer `5` is an expression within that statement. A declaration isn't an expression, so you can't place a declaration where C# expects a value.

An *assignment expression* stores a value in a variable, property, indexer, or other storage location. C# permits an assignment expression to form an *expression statement*:

```csharp
quantity = 10;
```

This statement performs an action rather than merely calculating a value. Method calls and increment operations are other common expression statements:

```csharp
Console.WriteLine("Restocking");
quantity++;
```

Only the following expression forms can be expression statements:

- Assignment expressions
- Method invocation expressions
- Object creation expressions
- Prefix or postfix increment and decrement expressions
- `await` expressions

Not every expression can stand alone as a statement. For example, `quantity + 1;` computes a value but doesn't use it, so the compiler reports [Compiler Error CS0201](../../language-reference/compiler-messages/cs0201.md).

<!-- Remove this HTML comment after dotnet/docs#55469 is merged and this branch is rebased. For more information, see the [Expressions overview](../expressions/index.md). -->

## Group statements in blocks

A *block* groups zero or more statements between braces (`{` and `}`). C# treats the group as one statement. In the opening example, the `if` statement can run both the assignment and the call to `Console.WriteLine` because a block groups them into one body. Blocks can nest inside other blocks.

Selection and iteration statements call their body an *embedded statement*. That body can be one statement without braces or a block that groups multiple statements.

Prefer a block even when a body contains only one statement. Braces show which statements belong to the body and prevent later edits from accidentally placing a statement outside it.

### Blocks and variable scope

Variables declared in a block are in scope from their declaration through the end of that block. A nested block can use variables declared by an enclosing block, but the enclosing block can't use variables declared only in the nested block:

:::code language="csharp" source="./snippets/statements-overview/Program.cs" id="BlocksAndScope":::

## Choose a statement for the task

After you recognize statements as commands, you can choose among their different kinds by purpose:

- **Declare data:** [Declaration statements](../../language-reference/statements/declarations.md) introduce local variables and constants.
- **Perform actions:** Expression statements assign values, call methods, create objects, increment or decrement values, or await asynchronous operations.
- **Choose steps:** [Selection statements](selection.md), such as `if` and `switch`, choose which code runs.
- **Repeat steps:** [Iteration statements](iteration.md), such as `foreach`, `while`, and `for`, repeat a statement or block.
- **Transfer control:** [Jump statements](../../language-reference/statements/jump-statements.md), such as `break`, `continue`, `return`, and `yield`, move execution to another point.
- **Handle exceptions:** [Exception-handling statements](../../language-reference/statements/exception-handling-statements.md), such as `try`, `catch`, and `throw`, respond to or report errors.
- **Manage resources:** The [`using` statement](../../language-reference/statements/using.md) ensures that resources are disposed.
- **Use specialized behavior:** The [`checked` and `unchecked`](../../language-reference/statements/checked-and-unchecked.md), [`fixed`](../../language-reference/statements/fixed.md), and [`lock`](../../language-reference/statements/lock.md) statements support specific scenarios.

## Less common statements

The *empty statement* is a lone semicolon:

```csharp
;
```

It performs no action. An empty statement is legal where C# expects a statement, but a stray semicolon after an `if`, `while`, or `for` can create an empty body and cause unexpected behavior. Use an empty statement only when the no-op is intentional and clear.

## C# language specification

For more information, see the [Statements](~/_csharpstandard/standard/statements.md) section of the [C# language specification](~/_csharpstandard/standard/README.md).

## See also

- [Statement keywords](../../language-reference/keywords/statement-keywords.md)
- [C# operators and expressions](../../language-reference/operators/index.md)
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace StatementsOverview;

public static class Program
{
public static void Main()
{
ShowStatementRecipe();
ShowBlocksAndScope();
}

private static void ShowStatementRecipe()
{
// <StatementRecipe>
int quantity = 5;
Console.WriteLine($"Quantity: {quantity}"); // => Quantity: 5

if (quantity < 10)
{
quantity = 10;
Console.WriteLine("Restocked"); // => Restocked
}

Console.WriteLine($"Quantity: {quantity}"); // => Quantity: 10
// </StatementRecipe>
}

private static void ShowBlocksAndScope()
{
// <BlocksAndScope>
int outerValue = 10;

if (outerValue > 0)
{
int innerValue = outerValue * 2;
Console.WriteLine(innerValue); // => 20
}

// innerValue isn't in scope here.
// </BlocksAndScope>
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ Call `PerformSurvey` from `Main`:

## Examine the survey results

To report results, expose a few helpers from `SurveyResponse` and `SurveyRun`. On `SurveyResponse`, add [expression-bodied members](../../programming-guide/statements-expressions-operators/expression-bodied-members.md) (members defined with `=>` and a single expression instead of a `{ ... }` block) that handle the nullable dictionary:
To report results, expose a few helpers from `SurveyResponse` and `SurveyRun`. On `SurveyResponse`, add [expression-bodied members](../../language-reference/operators/lambda-operator.md#expression-body-definition) (members defined with `=>` and a single expression instead of a `{ ... }` block) that handle the nullable dictionary:

:::code language="csharp" source="snippets/NullableIntroduction/SurveyResponse.cs" id="SnippetSurveyStatus":::

Expand Down
4 changes: 4 additions & 0 deletions docs/csharp/fundamentals/types/built-in-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ Use `dynamic` when interacting with COM APIs, dynamic languages, or reflection-h

## See also

<!-- Remove this HTML comment after dotnet/docs#55469 is merged and this branch is rebased.
- [Operators](../expressions/operators.md)
-->

- [Type system overview](index.md)
- [Built-in types (C# reference)](../../language-reference/builtin-types/built-in-types.md)
- [Integral numeric types](../../language-reference/builtin-types/integral-numeric-types.md)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ ms.assetid: cf5d6701-50cc-4e4f-878b-e1a4ad8a2061

Only assignment, call, increment, decrement, and new object expressions can be used as a statement

The compiler generates an error when it encounters an invalid statement. An invalid statement is any line or series of lines ending in a semicolon that does not represent an assignment ([=](../operators/assignment-operator.md)), method call [()](../operators/member-access-operators.md#invocation-expression-), [new](../operators/new-operator.md), [--](../operators/arithmetic-operators.md#decrement-operator---) or [++](../operators/arithmetic-operators.md#increment-operator-) operation. For more information, see [Statements](../../programming-guide/statements-expressions-operators/statements.md) and [Operators and expressions](../operators/index.md).
The compiler generates an error when it encounters an invalid statement. An invalid statement is any line or series of lines ending in a semicolon that does not represent an assignment ([=](../operators/assignment-operator.md)), method call [()](../operators/member-access-operators.md#invocation-expression-), [new](../operators/new-operator.md), [--](../operators/arithmetic-operators.md#decrement-operator---) or [++](../operators/arithmetic-operators.md#increment-operator-) operation. For more information, see [Statements](../../fundamentals/statements/index.md) and [Operators and expressions](../operators/index.md).

## Example 1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ helpviewer_keywords:
---
# Statement keywords (C# Reference)

Statements are program instructions. Except as described in the topics referenced in the following list, the program executes statements in sequence. The following list shows the C# statement keywords. For more information about statements that don't use a keyword, see [Statements](../../programming-guide/statements-expressions-operators/statements.md).
Statements are program instructions. Except as described in the topics referenced in the following list, the program executes statements in sequence. The following list shows the C# statement keywords. For more information about statements that don't use a keyword, see [Statements](../../fundamentals/statements/index.md).

- [Selection statements](../statements/selection-statements.md)
- `if`
Expand Down Expand Up @@ -40,5 +40,5 @@ Statements are program instructions. Except as described in the topics reference

## See also

- [Statements](../../programming-guide/statements-expressions-operators/statements.md)
- [Statements](../../fundamentals/statements/index.md)
- [C# Keywords](index.md)
2 changes: 1 addition & 1 deletion docs/csharp/language-reference/operators/default.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ You can use the `default` literal to produce the default value of a type when th
- In the assignment or initialization of a variable.
- In the declaration of the default value for an [optional method parameter](../../methods.md#optional-parameters-and-arguments).
- In a method call to provide an argument value.
- In a [`return` statement](../statements/jump-statements.md#the-return-statement) or as an expression in an [expression-bodied member](../../programming-guide/statements-expressions-operators/expression-bodied-members.md).
- In a [`return` statement](../statements/jump-statements.md#the-return-statement) or as an expression in an [expression-bodied member](lambda-operator.md#expression-body-definition).

The following example shows the usage of the `default` literal:

Expand Down
4 changes: 2 additions & 2 deletions docs/csharp/language-reference/operators/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ In the following code, examples of expressions appear on the right-hand side of

:::code language="csharp" source="snippets/shared/Overview.cs" id="Expressions":::

Typically, an expression produces a result and can be included in another expression. A [`void`](../builtin-types/void.md) method call is an example of an expression that doesn't produce a result. It can be used only as a [statement](../../programming-guide/statements-expressions-operators/statements.md), as the following example shows:
Typically, an expression produces a result and can be included in another expression. A [`void`](../builtin-types/void.md) method call is an example of an expression that doesn't produce a result. It can be used only as a [statement](../../fundamentals/statements/index.md), as the following example shows:

```csharp
Console.WriteLine("Hello, world!");
Expand All @@ -52,7 +52,7 @@ Here are some other kinds of expressions that C# provides:

:::code language="csharp" source="snippets/shared/Overview.cs" id="Query":::

You can use an [expression body definition](../../programming-guide/statements-expressions-operators/expression-bodied-members.md) to provide a concise definition for a method, constructor, property, indexer, or finalizer.
You can use an [expression body definition](lambda-operator.md#expression-body-definition) to provide a concise definition for a method, constructor, property, indexer, or finalizer.

## Operator precedence

Expand Down
Loading
Loading