Switch expressions are used extensively to solve pattern matching problems, and are very similar to switch statements. They were introduced in C# 8.0.
A switch
expression is identified by the switch
keyword, and its return value depends on the case label matched. If no case is matched, the value in the default case is returned.
A switch
expression can evaluate variables that contain multiple values, such as a tuple
, and single-valued variables, such as numerical values of type int
.
It must be noted that a switch
expression:
break
statement.A switch
expression evaluates such an input based on a pre-defined template because the data type of the input must match the data type of the switch case labels. The _
operator indicates that any value is acceptable.
The =>
operator identifies the return value of a particular case and is placed before the definition of the input template, as demonstrated by the program below.
Switch expressions also support optional case guards, which are used to specify conditions in the case expression:
("Cricket", "Football", "Swimming",_) when num > 1 => "I like Cricket, Football, and Swimming.",
The case above will only execute if the value of num is greater than 1, as specified by the when
keyword.
Since the patterns specified in a switch expression are exhaustive, an exception is thrown at run-time if no case is matched. A warning is generated if the switch expression fails to cater to all possible inputs.
The following program declares a function named sports_i_like
, which takes in one tuple
as its parameter. It compares this tuple
with tuple
templates defined as switch expression cases, and returns the corresponding string
if a case label is matched. If no case is matched, the default case is executed.
Using the Controle.WriteLine
function, we output the return value of the function:
using System;public class Program {// Main functionpublic static void Main(){// define function containng a switch expressionstatic string sports_i_like(string sport1, string sport2, string sport3, int num)//denoting input to the switch expression, which is a tuple of three strings=> (sport1, sport2, sport3, num) switch{//matches if Cricket, Football, and Swimming given as input("Cricket", "Football", "Swimming",_) when num > 1 => "I like Cricket, Football, and Swimming.",//matches if Cricket, Football, and Baseball given as input("Cricket", "Football", "Baseball",_) when num > 1 => "I like Cricket, Football, and Baseball.",//matches if Hockey, Football, and Swimming given as input("Hockey", "Football", "Swimming",_) when num > 1 => "I like Hockey, Football, and Swimming.",//matches if Table Tennis, Football, and Swimming given as input("Table Tennis", "Football", "Swimming",_) when num > 1 => "I like Table Tennis, Football and Swimming.",//Default case(_, _, _, _) => "Invalid input!"};Console.WriteLine(sports_i_like("Cricket", "Football", "Swimming",2));Console.WriteLine(sports_i_like("Cricket", "Football", "Racing",2));}}
The input of the program is as follows, as the first call matches the first case label and the second call results in the default case:
I like Cricket, Football, and Swimming.
Invalid input.
Note that
Racing
is not part of any case label.
Free Resources