If Statements in C#
From the C# Programming Language cheat sheet · Control Flow · verified Jul 2026
If Statements
Conditional execution
csharp
// Basic if
if (age >= 18)
{
Console.WriteLine("Adult");
}
// If-else
if (score >= 90)
{
Console.WriteLine("A");
}
else if (score >= 80)
{
Console.WriteLine("B");
}
else
{
Console.WriteLine("C or below");
}
// Ternary operator
string status = age >= 18 ? "Adult" : "Minor";
// Null checking
if (name != null)
{
Console.WriteLine(name.Length);
}💡 Use && for AND, || for OR, ! for NOT
⚡ Ternary operator (? :) for simple if-else
📌 Always use braces {} even for single statements
🟢 Pattern matching makes type checking cleaner