C# — If Else
Basic if statement
using System;
int temperature = 28;
if (temperature > 25)
{
Console.WriteLine("It's warm outside!");
}
if-else
using System;
int hour = 14;
if (hour < 12)
{
Console.WriteLine("Good morning!");
}
else
{
Console.WriteLine("Good afternoon!");
}
if-else if-else
using System;
int score = 85;
if (score >= 90)
{
Console.WriteLine("Grade: A");
}
else if (score >= 80)
{
Console.WriteLine("Grade: B");
}
else if (score >= 70)
{
Console.WriteLine("Grade: C");
}
else
{
Console.WriteLine("Grade: F");
}
Ternary operator
using System;
int age = 20;
string status = (age >= 18) ? "Adult" : "Minor";
Console.WriteLine(status); // Adult
Switch expression (C# 8+)
using System;
int day = 3;
string dayName = day switch
{
1 => "Monday",
2 => "Tuesday",
3 => "Wednesday",
4 => "Thursday",
5 => "Friday",
6 => "Saturday",
7 => "Sunday",
_ => "Invalid day"
};
Console.WriteLine(dayName); // Wednesday
Pattern matching in switch
using System;
object obj = 42;
string description = obj switch
{
int i when i > 0 => $"Positive int: {i}",
int i => $"Non-positive int: {i}",
string s => $"String of length {s.Length}",
null => "Null",
_ => "Unknown type"
};
Console.WriteLine(description);
Type pattern matching
using System;
static string Describe(object obj)
{
return obj switch
{
int n when n > 0 => $"Positive: {n}",
int => "Non-positive integer",
string s => $"String: \"{s}\"",
bool b => b ? "True" : "False",
double d => $"Double: {d}",
null => "Null",
_ => $"Other: {obj.GetType().Name}"
};
}
Console.WriteLine(Describe(42)); // Positive: 42
Console.WriteLine(Describe("Hi")); // String: "Hi"
Console.WriteLine(Describe(null)); // Null
Property pattern
using System;
record Person(string Name, int Age);
string Classify(Person p) => p switch
{
{ Age: < 13 } => "Child",
{ Age: < 18 } => "Teenager",
{ Age: < 65 } => "Adult",
_ => "Senior"
};
Console.WriteLine(Classify(new("Alice", 30))); // Adult
Console.WriteLine(Classify(new("Bob", 10))); // Child
Combining conditions
using System;
int age = 25;
int income = 50000;
if (age >= 18 && income >= 30000)
{
Console.WriteLine("Qualifies for premium card.");
}
if (age < 12 || age > 65)
{
Console.WriteLine("Discounted ticket.");
}
bool isBanned = false;
if (!isBanned)
{
Console.WriteLine("Access granted.");
}
If with declaration (C# 8+)
using System;
// Declare variable in if condition
if (int.TryParse("42", out int result))
{
Console.WriteLine($"Parsed: {result}");
}
// Also works with pattern matching
if ("hello" is string { Length: > 3 } s)
{
Console.WriteLine($"Long string: {s}");
}
Best practices
- Use pattern matching instead of type checking + casting
- Use switch expressions for cleaner code
- Prefer early returns to reduce nesting
- Use
isfor null checking
Mini Practice
Write C# code that:
- Uses a switch expression to classify a number
- Demonstrates pattern matching with
whenclauses - Uses
ifwith variable declaration - Combines conditions with
&&and||
Up Next
In the next lesson, you'll learn about Loops — for, foreach, while, and do-while.
Related Topics
Frequently Asked Questions about If Else
What is If Else in C#?
If Else is a fundamental concept in C#. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn If Else?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn If Else.
Why is If Else important in C#?
If Else is essential for C# development. Understanding this concept will help you write better code and solve real-world problems more effectively.