Conditions and Switch Expressions
Switch expressions map an input to a result and require every relevant case to be handled.
How it works
Choose behavior with if statements and concise switch expressions.
Relational patterns make numeric ranges readable. The final discard pattern handles every remaining score.
Runnable C# example
Program.cs
using System;
class Program
{
static void Main()
{
int score = 87;
string grade = score switch
{
>= 90 => "A",
>= 80 => "B",
>= 70 => "C",
_ => "Needs practice"
};
Console.WriteLine(grade);
}
}
BPractice with the debugger
Set a breakpoint on an important assignment or condition, click Debug, and inspect the local values before stepping to the next line.