Integrating return and switch in C#: Evolution from Statements to Expressions

Dec 07, 2025 · Programming · 8 views · 7.8

Keywords: C# | switch expression | return statement | control flow | programming patterns

Abstract: This paper explores how to combine return statements with switch structures in C#, focusing on the switch expression feature introduced in C#8. By comparing traditional switch statements with switch expressions, it explains the fundamental differences between expressions and statements, and provides Dictionary mapping as a historical solution. The article details syntax improvements, application scenarios, and compatibility considerations of switch expressions, helping developers understand the evolution of control flow expressions in modern C#.

Introduction

In C# programming practice, the design of control flow structures directly impacts code conciseness and readability. Traditionally, combining switch as a statement with return statements often leads to code redundancy, requiring developers to repeat the return keyword in each case branch. This paper aims to explore how to achieve a single integration of return and switch, primarily referencing high-scoring Stack Overflow answers and analyzing the switch expression feature in C#8 in depth.

Limitations of Traditional Switch Statements

In C#7 and earlier versions, switch is defined as a statement, meaning it does not return a value and is only used for controlling program flow. For example:

switch (a)
{
    case 1: return "lalala";
    case 2: return "blalbla";
    case 3: return "lolollo";
    default: return "default";
}

This pattern requires each branch to include a return statement, resulting in code duplication. From a language design perspective, the key difference between statements and expressions is that expressions produce values and can be part of other expressions, while statements perform operations but do not return values. Therefore, traditional switch cannot be directly combined with a single return.

Historical Solution: Dictionary Mapping

Before C#8, developers often used Dictionary to achieve similar functionality as a workaround to avoid repeated return statements. Referencing the best answer, sample code is as follows:

var map = new Dictionary<int, string>() 
{
    {1, "lala"}, 
    {2, "lolo"}, 
    {3, "haha"}, 
};
string output;
return map.TryGetValue(a, out output) ? output : "default";

This method uses the TryGetValue method to check for key existence and returns the result via the ternary conditional operator. Advantages include concise code, but disadvantages include: additional memory for mapping storage, type safety relying on runtime checks, and unsuitability for complex pattern matching scenarios.

Innovation of C#8 Switch Expressions

C#8 introduced switch expressions, elevating switch from a statement to an expression, thus allowing direct combination with return. Syntax example:

return a switch
{
    1 => "lalala",
    2 => "blalbla",
    3 => "lolollo",
    _ => "default"
};

Key improvements include:

From a compiler perspective, switch expressions are compiled into efficient conditional jump logic, with performance comparable to traditional switch statements. Additionally, they support pattern matching, such as type patterns and property patterns, expanding application scenarios.

Technical Comparison and Best Practices

Comparing the three solutions:

<table border="1"> <tr><th>Solution</th><th>Syntax Conciseness</th><th>Performance</th><th>Compatible Versions</th></tr> <tr><td>Traditional Switch Statement</td><td>Low (repeated return)</td><td>High</td><td>C#1+</td></tr> <tr><td>Dictionary Mapping</td><td>Medium</td><td>Medium (hash table overhead)</td><td>C#2+</td></tr> <tr><td>Switch Expression</td><td>High</td><td>High</td><td>C#8+</td></tr>

In practical development, it is recommended to:

  1. For C#8 and above projects, prioritize switch expressions to improve code readability and maintainability.
  2. In older versions, use traditional switch statements for few branches; consider Dictionary solutions for complex mappings.
  3. Note that switch expressions require all branches to cover possible values, otherwise the compiler will report an error, enhancing type safety.

Conclusion

By analyzing the integration of return and switch in C#, this paper reveals the evolutionary trend from statements to expressions in the language. Switch expressions, as a core feature of C#8, not only address syntax redundancy but also introduce advanced features like pattern matching. Developers should understand the fundamental differences between expressions and statements and choose appropriate solutions based on project needs. In the future, with C# version updates, the expression-oriented control flow may become mainstream, driving code towards a more declarative style.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.