Lets check out the various types of statements in C#.
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.
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 flower brackets and can
contain nested blocks.
Now lets take a look at the various types of statements in C#.
Declaration Statements:
The following code shows examples of variable declarations.
One without initial assignment.
And one with initial assignment.
Expression Statements:
The following code shows example of expression statement.
Method invocation statements and object creation statements
are also treated as expression
Statements.
Selection Statements:
Selection statements enable you to branch to different sections of code, depending on one or more
specified conditions.
Examples include if else
statements. switch statements. and case statements.
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.
Examples are for, foreach
and while statements.
Jump Statements:
Jump statements transfer
control to another section of code.
Exception Handling Statements:
Exception handling
statements enable you to gracefully recover from exceptional conditions that
occur at run time.
Checked and Unchecked Statements:
C# statements can execute in either checked or unchecked
context. In a checked context, arithmetic overflow raises an exception. In an
unchecked context, arithmetic overflow is ignored and the result is truncated
by discarding any high-order bits that don't fit in the destination type.
The await Statement:
If you mark a method with the async modifier, you can use the await operator in the method. When
in the method is suspended until the awaited task completes.
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.
The fixed Statement:
The fixed statement prevents the garbage collector from relocating a movable variable. The fixed statement is only permitted in an unsafe context.
The lock Statement:
The lock statement acquires the mutual-exclusion lock for a given object, executes a statement block, and then releases the lock. While a lock is held, the thread that holds the lock can again acquire and release the lock. Any other thread is blocked from acquiring the lock and waits until the lock is released.
Labeled Statement:
You can give a statement
a label and then use the goto keyword to jump to the labeled statement.
Empty Statements:
And finally, 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.
No comments:
Post a Comment