C# statement keywords are reserved words in the C# programming language that define various control flow and looping constructs, as well as exception-handling mechanisms. These keywords are used to create structured and responsive code by allowing the program to execute different blocks of code based on certain conditions and to handle exceptions gracefully.
Here are the C# statement keywords:
Keyword | Description |
---|---|
if |
Executes a block of code if a specified condition is true. |
else |
Executes a block of code if the preceding 'if' condition is false. |
switch |
Evaluates a variable and performs different actions based on its value. |
case |
Defines specific values or ranges within a 'switch' statement. |
do |
Executes a block of code repeatedly until a specified condition is true. |
for |
Repeats a block of code a specified number of times. |
foreach |
Iterates over elements in a collection or array. |
in |
Used in 'foreach' loops to specify the collection to iterate over. |
while |
Repeats a block of code while a specified condition is true. |
break |
Exits the current loop or 'switch' statement. |
continue |
Skips the current iteration of a loop and continues to the next. |
default |
Specifies the default case in a 'switch' statement. |
goto |
Transfers control to a labeled statement in the code. |
return |
Exits a method and returns a value to the caller. |
yield |
Used with 'IEnumerable' to return a sequence of values. |
throw |
Raises an exception to signal an error or exceptional condition. |
try |
Starts a block of code that might cause an exception. |
catch |
Catches and handles exceptions that occur in a 'try' block. |
finally |
Defines a block of code that executes regardless of exceptions. |
checked |
Checks for arithmetic overflow during computations. |
unchecked |
Disables overflow checking during arithmetic computations. |
fixed |
Declares a pointer variable and pins it to a memory location. |
lock |
Provides a way to synchronize access to a shared resource in a multithreaded environment. |