C# Switch Case with 💻Lamda Expression
3 min readSep 13, 2023
In C#, the switch
statement is used for conditional branching based on the value of an expression. It allows you to compare the expression against multiple possible constant values (cases) and execute the code block associated with the first matching case. The switch
statement provides a more concise way to handle multiple conditional branches compared to using a series of if-else if
statements.
Switch Case: Before Using Lambda Expressions
public static class WithoutLambdaExpressions
{
public static string GetLetterGrade(int numericalGrade)
{
string letterGrade;
switch (numericalGrade)
{
case int n when n >= 90:
letterGrade = "A";
break;
case int n when n >= 80:
letterGrade = "B";
break;
case int n when n >= 70:
letterGrade = "C";
break;
case int n when n >= 60:
letterGrade = "D";
break;
default:
letterGrade = "F";
break;
}
return letterGrade;
}
}
In this example:
- The program prompts the user to enter a…