Can I write switch case in c# like this?
switch (string)
case [a..z]+
// do something
case [A..Z]+
// do something
....
Yes you can in C# 7 (and nobody noticed I had used the incorrect range character in the character class .. instead of -). Updated now with a slightly more useful example that actually works:
using System.Text.RegularExpressions;
string[] strings = {"ABCDEFGabcdefg", "abcdefg", "ABCDEFG"};
Array.ForEach(strings, s => {
switch (s)
{
case var someVal when new Regex(@"^[a-z]+$").IsMatch(someVal):
Console.WriteLine($"{someVal}: all lower");
break;
case var someVal when new Regex(@"^[A-Z]+$").IsMatch(someVal):
Console.WriteLine($"{someVal}: all upper");
break;
default:
Console.WriteLine($"{s}: not all upper or lower");
break;
}
});
Output:
ABCDEFGabcdefg: not all upper or lower
abcdefg: all lower
ABCDEFG: all upper
A minor refinement on David's excellent answer using _ as a discard. Just a very simple string value as an example.
using System;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
string userName = "Fred";
int something = 0;
switch (true)
{
case bool _ when Regex.IsMatch(userName, @"Joe"):
something = 1;
break;
case bool _ when Regex.IsMatch(userName, @"Fred"):
something = 2;
break;
default:
break;
}
Console.WriteLine(something.ToString());
}
}
Starting with C# 8.0, the answer is now YES!... sort of.
With 8.0, the switch expression was introduced. You can use that to match many different styles of patterns as show below.
string myValueBeingTested = "asdf";
string theResultValue = myValueBeingTested switch
{
var x when
Regex.IsMatch(x, "[a..z]+") => "everything is lowercase",
var y when
Regex.IsMatch(y, "[A..Z]+") => "EVERYTHING IS UPPERCASE",
"Something Specific" => "The Specific One",
_ => "The default, no match scenario"
};
How it works: The switch expression needs a variable that it can feed the results into. The above sample uses theResultValue variable for that. Next you pass into the switch what is referred to as the range expression represented by myValueBeingTested above. The familiar case construct is replaced with a pattern expression followed by the => token and together referred to as an expressionn arm. Anything on the right side of the arm token will end up in the result variable.
Normally the switch expression requires that patterns be a compile-time constant, and you cannot execute arbitrary code inside a switch expression. However we can use a case gaurd pattern by using the when clause to make use of Regex as shown above. Each case needs to be wired up in a distinct arm with distinct throw-away variables, (x and y above).
The 3rd arm shown above is a compile-time constant string to match some specific text if desired. The 4th arm shown above uses the _ pattern, which is a sort of wildcard similar to the old default: keyword. You can also use null instead of, or in addition to _ to handle null cases separately from anything else.
Try it out in my .NET Fiddle sample here.