Edit

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 tutorials first. For complete statement syntax, see Statements 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:

int quantity = 5;
Console.WriteLine($"Quantity: {quantity}"); // => Quantity: 5

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

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

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:

int quantity = 5;

The complete line is a declaration statement. The initializer 5 is an expression within that statement.

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:

quantity = 10;

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

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. It's not a valid statement.

Here's a useful working definition: a statement is roughly like a complete sentence in English, while an expression is like a phrase. This distinction helps you understand the formal terminology and explanations you'll encounter in documentation and specifications, though ordinary coding rarely requires you to remember the distinction consciously.

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 define 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:

int outerValue = 10;

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

// innerValue isn't in scope here.

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 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, such as if and switch, choose which code runs.
  • Repeat steps: Iteration statements, such as foreach, while, and for, repeat a statement or block.
  • Transfer control: Jump statements, such as break, continue, return, and yield, move execution to another point.
  • Handle exceptions: Exception-handling statements, such as try, catch, and throw, respond to or report errors.
  • Manage resources: The using statement ensures that resources are disposed.
  • Use specialized behavior: The checked and unchecked, fixed, and lock statements support specific scenarios.

C# language specification

For more information, see the Statements section of the C# language specification.

See also