(C# Language) Statements and Expressions

The concept of statements and expressions in C# is exactly the same as in other programming languages. So, this tutorial is a review for the sake of completeness. We have discussed Declaration Statements, Expression Statements, Empty Statement, Nested Statement Blocks, Selection Statements, Iteration Statements, Jumping Statements, Exception Statements, await Statements, fixed Statements and lock Statements.
(Rev. 18-Jun-2024)

Categories | About |     |  


Table of Contents (top down ↓)

Statements express actions

Your program takes actions through statements. Declaration of a variable is an action. Assignment is an action, looping is an action, calling a method is an action, and so on.

A statement can be a single line of code ending in a semi-colon. For example, char[] separators = { ',', ';', ' ' }; is a declaration statement with initializers.

A statement can be in the form of a block of single-line statements. An example is a for-loop for(int i = 0; i < 10; i++) { Console.WriteLine(i); Console.WriteLine(i * 10); } .

for(int i = 0; i < 10; i++)

  Console.WriteLine(i * 10);

This statement is a block statement consisting of two statements.

A block statement is enclosed in curly braces.

Video Explanation (see it happen!)

Please watch the following youtube video:

Declaration Statements

A declaration statement declares a variable. It can assign an initial value.

float area;

int counter = 0;

Expression Statements

An expression statement calculates a value and should store it in a variable int side = 4; int volume = 3 * side;

// expression statement - calculation+assignment 
int side = 4;

int volume = 3 * side;

An expression statement can make a function call.

// expression statement - function call 
Console.WriteLine("Hello, World!");

An expression statement can create a new object List<String> strings = new List<String>();

// expression statement - create a new object 
List<String> strings = new List<String>();

Empty Statement

A semicolon is an empty statement. It is often required to close a loop statement that has no blocks. while(side > 4) ;

// empty statement 
while(side > 4)

It is also required in an empty label - such as an empty default case label of a switch.

Nested Statement Blocks

Statement blocks can be nested also. For example, this for loop contains a nesting of if statements.

// nested statement blocks 
for(int i = 0; i < 10; i++)
  if(i < 5)
    if(0 == i %2)
      // any statements here 

Selection Statements

Decisions are made with selection statements.

Examples of selection statements are an if statement and a switch statement.

Iteration Statements

Looping is done with iteration statements.

Examples are do, for, foreach and while statements.

Jumping Statements

Flow of a program can be manipulated with jumps to another part of your program.

Examples are break, continue, goto, return and yield statements.

Exception Statements

Exception handling is done with exception statements.

Examples are throw, catch, try-catch, try-finally and try-catch-finally.

await Statements

await statements are used in asynchronous programming.

fixed Statements

fixed statements are used to prevent the garbage collector from moving your object.

For example this code uses pointers in an unsafe block. The fixed statement will prevent the garbage collector from moving the byte array.

// use of pointers 
  byte[] bytes = { 0xe, 0x12, 0x45 };

  fixed (byte* pointer = bytes)
    // statements 

lock Statements

A lock statement is a block that prevents multi-threaded access to a variable.

String y = "app name";

// prevent multi-threaded access to y 


This Blog Post/Article "(C# Language) Statements and Expressions" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.