Switch expression and string interpolation

Switch expressions inside string interpolation expression with C#11

Home DailyDrop

Daily Knowledge Drop

C# 11 is introducing the ability to use switch expressions inside a string interpolation expression.

This allows for more simplified code, as instead of creating a separate method to be used once-off when generating the string, all the string building logic can be contained in a single place.


Use case

In our use case, we want to convert a numerical grade (e.g. 89%), to a string representation ("A" in this case). We also want to be able to output both of these values in a string.


Separate expression - pre C# 11

Prior to C# 11 it was not possible for interpolation expressions to be switch expressions directly - the expression had to be a separate method:

// method which converts a numeric grade value
// to a string representation
public string GetGradeAsString(int grade) => grade switch
{
    > 90 => "A+",
    > 80 => "A",
    > 70 => "B",
    > 60 => "C",
    > 50 => "D",
    > 40 => "E",
    _ => "F"
};

var studentGrade = 89;

// using string interpolation, and calling the method defined
Console.WriteLine($"{studentGrade} converted to " +
    $"a string is {GetGradeAsString(studentGrade)}");

There is nothing wrong with this approach, and it will continue to work with C# 11 - however the issue here, is that if the conversion is only ever required once (when outputting the data), a method was required to be defined.

Is defining a method a big deal? No Will it create massive overhead? No Is there a slightly easier way in C# 11? Yes.


Embedded expression - C# 11

C# 11 allows for switch expressions to be used directly in interpolated strings:

var studentGrade = 89;

// a separate method is not required
Console.WriteLine($"{studentGrade} converted to a string is {
    studentGrade switch
    {
        > 90 => "A+",
        > 80 => "A",
        > 70 => "B",
        > 60 => "C",
        > 50 => "D",
        > 40 => "E",
        _ => "F"
    }}");

The same outcome as below, just slightly more concise and contained code.


Notes

This is not a massive enhancement, but it does help reduce unnecessary ceremony (creating a method) when not required - ultimately resulting in cleaner, more concise and quicker to write code when the use case applies.


References

What’s new in C# 11? Dev friendly features

Daily Drop 195: 04-11-2022

At the start of 2022 I set myself the goal of learning one new coding related piece of knowledge a day.
It could be anything - some.NET / C# functionality I wasn't aware of, a design practice, a cool new coding technique, or just something I find interesting. It could be something I knew at one point but had forgotten, or something completely new, which I may or may never actually use.

The Daily Drop is a record of these pieces of knowledge - writing about and summarizing them helps re-enforce the information for myself, as well as potentially helps others learn something new as well.
c# .net string interpolation switch .net7