# C# Switch Statements: A Comparison of Traditional and Expression Syntax
Written on
Chapter 1: Introduction to Switch Statements
In C# 8.0, Microsoft unveiled a more streamlined and readable syntax known as the switch expression, enhancing both code clarity and efficiency. This article aims to compare this fresh expression syntax with the traditional approach and highlight their differences.
Why is it Called a Switch Expression?
The term "switch expression" emphasizes that this new syntax is designed to return a result directly, rather than executing multiple steps. This aligns it more closely with functional programming paradigms, allowing developers to write shorter and clearer code.
Advantages of the New Syntax
- Produces cleaner and more comprehensible code.
- The compiler provides warnings for any missing cases.
- Allows for sophisticated and expressive matching.
Disadvantages of the New Syntax
- May not be ideal for scenarios requiring multiple actions.
Chapter 2: Syntax Comparison
First, let's examine the differences in syntax. The input variable is what we are switching on, while a and b represent potential matches. The remaining elements are simply the switch syntax.
Traditional Syntax:
switch(input)
{
case a:
// code block
break;
case b:
// code block
break;
default:
// default code block
}
New Syntax:
var result = input switch
{
a => // code block,
b => // code block,
_ => // default code block
};
To illustrate the difference, consider a function that returns the meaning of a letter grade. We have an enumeration for letter grades, named LetterGrade:
public enum LetterGrade
{
A,
B,
C,
D,
F
}
Traditional Switch Statement Example
Here’s how the logic would be structured using the traditional switch statement:
public static string GetGradeMeaning(LetterGrade grade)
{
string meaning;
switch (grade)
{
case LetterGrade.A:
meaning = "Excellent";
break;
case LetterGrade.B:
meaning = "Good";
break;
case LetterGrade.C:
meaning = "Average";
break;
case LetterGrade.D:
meaning = "Below Average";
break;
case LetterGrade.F:
meaning = "Failing";
break;
default:
throw new ArgumentException("Invalid letter grade");}
return meaning;
}
Here’s another version of the traditional switch statement that aligns more closely with the new syntax:
public static string GetGradeMeaning(LetterGrade grade)
{
switch (grade)
{
case LetterGrade.A:
return "Excellent";case LetterGrade.B:
return "Good";case LetterGrade.C:
return "Average";case LetterGrade.D:
return "Below Average";case LetterGrade.F:
return "Failing";default:
throw new ArgumentException("Invalid letter grade");}
}
New Switch Expression Example
Now let’s see how the new switch expression looks:
public static string GetGradeMeaning(LetterGrade grade)
{
return grade switch
{
LetterGrade.A => "Excellent",
LetterGrade.B => "Good",
LetterGrade.C => "Average",
LetterGrade.D => "Below Average",
LetterGrade.F => "Failing",
_ => throw new ArgumentException("Invalid letter grade")
};
}
Chapter 3: When to Use Traditional Switch Statements
Traditional switch statements are more suitable when you have multiple complex actions that need to be performed within each case block. Using the traditional syntax can simplify your code instead of complicating it with switch expressions.
Example of a Traditional Syntax Scenario:
public static void ProcessStudentDocument(DocumentType docType, string filePath)
{
switch (docType)
{
case DocumentType.Text:
// Load text file
string textContent = File.ReadAllText(filePath);
// Perform some text processing
int wordCount = textContent.Split(' ').Length;
// Log the result
Console.WriteLine($"Word count: {wordCount}");
break;
case DocumentType.Image:
// Load image file
using var image = Image.FromFile(filePath);
// Perform some image processing
using var resizedImage = new Bitmap(image, new Size(100, 100));
// Save the resized image
resizedImage.Save("resized_image.jpg");
break;
case DocumentType.Video:
// Load video file
var video = new Video(filePath);
// Perform some video processing
for (int i = 0; i < video.FrameCount; i++)
{
// Do Something}
break;
default:
throw new ArgumentException("Invalid document type");}
}
Thank you for reading! If you're interested in more content like this, consider following me on Medium. Have you joined our community yet? Sign up today to become a member! Let's connect on Twitter to stay updated!
The video titled "C Programming Tutorial for Beginners" offers insights into foundational programming concepts, perfect for novices looking to grasp the basics.