2. Control Flow
1. Decision Making Statements
if Statement
int age = 18;
if (age >= 18)
{
Console.WriteLine("Adult");
}if (condition)
{
// Code executes if condition is true
}if-else Statement
int age = 15;
if (age >= 18)
{
Console.WriteLine("Adult");
}
else
{
Console.WriteLine("Minor");
}if-else-if Ladder
Nested if Statements
Ternary Operator (?:)
switch Statement (Traditional) - C# 1.0
switch Expressions - C# 8.0+
Pattern Matching in switch
Type Patterns (C# 7.0+)
Property Patterns (C# 8.0+)
Tuple Patterns (C# 8.0+)
Positional Patterns (C# 8.0+)
Relational Patterns (C# 9.0+)
Logical Patterns (C# 9.0+)
List Patterns (C# 11.0+)
Pattern Matching Evolution Table
Version
Feature
Example
2. Loops
for Loop
while Loop
do-while Loop
foreach Loop
Nested Loops
Loop Type
When to Use
3. Jump Statements
break
continue
return
goto (Discouraged)
throw
4. Arrays Deep Dive
Single-Dimensional Arrays
Multi-Dimensional Arrays (Rectangular)
Jagged Arrays (Array of Arrays)
Array Class Methods
Collection Expressions (C# 12.0+)
5. Strings Deep Dive
String Properties
String Methods Reference Table
Method
Description
Example
Result
String Manipulation
Concatenation
String Interpolation ($"")
Composite Formatting (String.Format)
String vs StringBuilder
String
StringBuilder
Escape Sequences
Common Loop Patterns
Sum of Numbers
Find Maximum
Count Occurrences
Reverse Array
Filter Array
String Manipulation Recipes
Split and Join
Remove Whitespace
Extract Substring
Check if String is Numeric
Capitalize First Letter
Reverse String
Common Pitfalls
1. Off-by-One Errors
2. Modifying Collection During foreach
3. String Concatenation in Loop
4. Forgetting break in switch
5. Infinite Loops
Best Practices
1. Use foreach When You Don't Need Index
2. Use Switch Expressions for Simple Mappings
3. Use Pattern Matching for Type Checks
4. Use String Interpolation Over Concatenation
5. Validate Loop Bounds
6. Use Collection Expressions (C# 12.0+)
7. Use Early Returns to Reduce Nesting
8. Use StringBuilder for Repeated Concatenation
Quick Reference Summary
Control Flow
Loops
Jump Statements
Arrays
Strings
Last updated