Skip to content

Latest commit

 

History

History
89 lines (57 loc) · 9.07 KB

File metadata and controls

89 lines (57 loc) · 9.07 KB
title description ms.date helpviewer_keywords ms.assetid
Statements
Learn about statements in C# programming. See a list of statement types, and view code examples and additional resources.
07/20/2015
statements [C#], about statements
C# language, statements
901bcde7-87de-4e15-833c-f9cfd40c8ce3

Statements (C# Programming Guide)

The actions that a program takes are expressed in statements. Common actions include declaring variables, assigning values, calling methods, looping through collections, and branching to one or another block of code, depending on a given condition. The order in which statements are executed in a program is called the flow of control or flow of execution. The flow of control may vary every time that a program is run, depending on how the program reacts to input that it receives at run time.

A statement can consist of a single line of code that ends in a semicolon, or a series of single-line statements in a block. A statement block is enclosed in {} brackets and can contain nested blocks. The following code shows two examples of single-line statements, and a multi-line statement block:

[!code-csharpcsProgGuideStatements#1]

Types of statements

The following table lists the various types of statements in C# and their associated keywords, with links to topics that include more information:

Category C# keywords / notes
Declaration statements A declaration statement introduces a new variable or constant. A variable declaration can optionally assign a value to the variable. In a constant declaration, the assignment is required.
Expression statements Expression statements that calculate a value must store the value in a variable.
Selection statements Selection statements enable you to branch to different sections of code, depending on one or more specified conditions. For more information, see the following topics:
Iteration statements Iteration statements enable you to loop through collections like arrays, or perform the same set of statements repeatedly until a specified condition is met. For more information, see the following topics:
Jump statements Jump statements transfer control to another section of code. For more information, see the following topics:
Exception-handling statements Exception-handling statements enable you to gracefully recover from exceptional conditions that occur at run time. For more information, see the following topics:
checked and unchecked The checked and unchecked statements enable you to specify whether integral-type numerical operations are allowed to cause an overflow when the result is stored in a variable that is too small to hold the resulting value.
The await statement If you mark a method with the async modifier, you can use the await operator in the method. When control reaches an await expression in the async method, control returns to the caller, and progress in the method is suspended until the awaited task completes. When the task is complete, execution can resume in the method.

For a simple example, see the "Async Methods" section of Methods. For more information, see Asynchronous Programming with async and await.
The yield return statement An iterator performs a custom iteration over a collection, such as a list or an array. An iterator uses the yield return statement to return each element one at a time. When a yield return statement is reached, the current location in code is remembered. Execution is restarted from that location when the iterator is called the next time.

For more information, see Iterators.
The fixed statement The fixed statement prevents the garbage collector from relocating a movable variable. For more information, see fixed.
The lock statement The lock statement enables you to limit access to blocks of code to only one thread at a time. For more information, see lock.
Labeled statements You can give a statement a label and then use the goto keyword to jump to the labeled statement. (See the example in the following row.)
The empty statement The empty statement consists of a single semicolon. It does nothing and can be used in places where a statement is required but no action needs to be performed.

Declaration statements

The following code shows examples of variable declarations with and without an initial assignment, and a constant declaration with the necessary initialization.

[!code-csharpcsProgGuideStatements#23]

Expression statements

The following code shows examples of expression statements, including assignment, object creation with assignment, and method invocation.

[!code-csharpcsProgGuideStatements#24]

The empty statement

The following examples show two uses for an empty statement:

[!code-csharpcsProgGuideStatements#25]

Embedded statements

Some statements, for example, iteration statements, always have an embedded statement that follows them. This embedded statement may be either a single statement or multiple statements enclosed by {} brackets in a statement block. Even single-line embedded statements can be enclosed in {} brackets, as shown in the following example:

[!code-csharpcsProgGuideStatements#26]

An embedded statement that is not enclosed in {} brackets cannot be a declaration statement or a labeled statement. This is shown in the following example:

[!code-csharpcsProgGuideStatements#27]

Put the embedded statement in a block to fix the error:

[!code-csharpcsProgGuideStatements#28]

Nested statement blocks

Statement blocks can be nested, as shown in the following code:

[!code-csharpcsProgGuideStatements#29]

Unreachable statements

If the compiler determines that the flow of control can never reach a particular statement under any circumstances, it will produce warning CS0162, as shown in the following example:

[!code-csharpcsProgGuideStatements#22]

C# language specification

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

See also