C# Expressions, Statements & Blocks: A Comprehensive Guide with Practical Examples
C# Expressions, Statements & Blocks: A Comprehensive Guide with Practical Examples
This article provides an in-depth look at C# expressions, statements, and blocks—explaining their roles, differences, and how they form the backbone of any C# program.
C# Expressions
An expression in C# is a combination of operands—such as variables, literals, or method calls—and operators that evaluates to a single value. While an expression must contain at least one operand, it may not include an operator.
Examples:
double temperature;
temperature = 42.05;
Here, 42.05 is an expression, and temperature = 42.05 is also an expression that assigns that value to temperature.
int a, b, c, sum;
sum = a + b + c;
In this case, a + b + c is an expression that computes the total.
if (age >= 18 && age < 58)
Console.WriteLine("Eligible to work");
The condition (age >= 18 && age < 58) evaluates to a boolean, and "Eligible to work" is a string expression used as the argument to WriteLine.
C# Statements
A statement is a fundamental unit of execution that performs an action. Programs are composed of multiple statements, each ending with a semicolon.
Examples:
int age = 21;
int marks = 90;
Both lines are declaration statements that introduce variables and optionally initialize them.
Key categories of statements include:
- Declaration Statements
- Expression Statements
Declaration Statements
Used to declare and optionally initialize variables.
char ch;
int maxValue = 55;
Both lines are declaration statements.
Expression Statements
An expression followed by a semicolon forms an expression statement.
// Assignment
area = 3.14 * radius * radius;
// Method call
System.Console.WriteLine("Hello");
Here, 3.14 * radius * radius is an expression; the assignment is an expression statement. The method call WriteLine is both an expression and a statement.
Other statement types—covered in later tutorials—include selection, iteration, jump, and exception‑handling statements.
For more details, see the official C# Statements (C# reference).
C# Blocks
A block groups zero or more statements within curly braces { } and defines a scope.
Example 1: Block with Statements
using System;
namespace Blocks
{
class BlockExample
{
public static void Main(string[] args)
{
double temperature = 42.05;
if (temperature > 32)
{
// Start of block
Console.WriteLine("Current temperature = {0}", temperature);
Console.WriteLine("It's hot");
}
// End of block
}
}
}
Running this program outputs:
Current temperature = 42.05
It's hot
The two WriteLine calls inside the braces form the block.
Example 2: Empty Block
Blocks can be empty or contain only comments.
using System;
namespace Blocks
{
class BlockExample
{
public static void Main(string[] args)
{
double temperature = 42.05;
if (temperature > 32)
{
// No statements
}
}
}
}
Here, the braces after the if contain no executable statements.
C Language
- Understanding Java Expressions, Statements, and Blocks
- Master C# Inheritance & Polymorphism: Practical Code Examples
- Mastering C# Queues: Enqueue, Dequeue, and First‑In‑First‑Out Operations Explained
- C# Hashtable Explained: Key-Value Storage, Operations, and Practical Examples
- Mastering C Conditional Statements: IF, IF-ELSE, and Nested IF-ELSE Explained
- While vs. Do‑While Loops: Clear Comparison with Practical Examples
- Python Loop Control: break, continue, and pass Statements Explained with Practical Examples
- Understanding type() and isinstance() in Python: Practical Examples
- Mastering C# Regular Expressions: A Practical Guide
- Precision Maintenance: Definition, Benefits, and Real‑World Examples