Skip to content

Latest commit

 

History

History
115 lines (65 loc) · 4.63 KB

File metadata and controls

115 lines (65 loc) · 4.63 KB
title C# Statements - A tour of the C# language
description You create the actions of a C# program using statements
keywords .NET, csharp, statements, syntax
author BillWagner
ms.author wiwagn
ms.date 11/06/2016
ms.topic article
ms.prod .net
ms.technology devlang-csharp
ms.devlang csharp
ms.assetid 5409c379-5622-4fae-88b5-1654276ea8d4

Statements

The actions of a program are expressed using statements. C# supports several different kinds of statements, a number of which are defined in terms of embedded statements.

A block permits multiple statements to be written in contexts where a single statement is allowed. A block consists of a list of statements written between the delimiters { and }.

Declaration statements are used to declare local variables and constants.

Expression statements are used to evaluate expressions. Expressions that can be used as statements include method invocations, object allocations using the new operator, assignments using = and the compound assignment operators, increment and decrement operations using the ++ and -- operators and await expressions.

Selection statements are used to select one of a number of possible statements for execution based on the value of some expression. In this group are the if and switch statements.

Iteration statements are used to execute repeatedly an embedded statement. In this group are the while, do, for, and foreach statements.

Jump statements are used to transfer control. In this group are the break, continue, goto, throw, return, and yield statements.

The try...catch statement is used to catch exceptions that occur during execution of a block, and the try...finally statement is used to specify finalization code that is always executed, whether an exception occurred or not.

The checked and unchecked statements are used to control the overflow-checking context for integral-type arithmetic operations and conversions.

The lock statement is used to obtain the mutual-exclusion lock for a given object, execute a statement, and then release the lock.

The using statement is used to obtain a resource, execute a statement, and then dispose of that resource.

The following lists the kinds of statements that can be used, and provides an example for each.

  • Local variable declaration:

[!code-csharpDeclarations]

  • Local constant declaration:

[!code-csharpConstantDeclarations]

  • Expression statement:

[!code-csharpExpressions]

  • if statement:

[!code-csharpIfStatement]

  • switch statement:

[!code-csharpSwitchStatement]

  • while statement:

[!code-csharpWhileStatement]

  • do statement:

[!code-csharpDoStatement]

  • for statement:

[!code-csharpForStatement]

  • foreach statement:

[!code-csharpForEachStatement]

  • break statement:

[!code-csharpBreakStatement]

  • continue statement:

[!code-csharpContinueStatement]

  • goto statement:

[!code-csharpGotoStatement]

  • return statement:

[!code-csharpReturnStatement]

  • yield statement:

[!code-csharpYieldStatement]

  • throw statements and try statements:

[!code-csharpTryThrow]

  • checked and unchecked statements:

[!code-csharpCheckedUncheckedStatement]

  • lock statement:

[!code-csharpLockStatement]

  • using statement:

[!code-csharpUsingStatement]

[!div class="step-by-step"] Previous Next