I admit it, I’ve struggled with pattern matching. In the next few blog posts I’ll explore this magic, starting today with switch expressions (not to be confused with switch statements).
The type of switch we’re familiar with is the switch statement:
namespace switchStatement;
internal class Program
{
static void Main(string[] args)
{
string day = string.Empty;
day = GetDay(2);
Console.WriteLine(day);
}
public static string GetDay(int dayNum)
{
string dayName;
switch (dayNum)
{
case 0:
dayName = "Sunday";
break;
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
case 4:
dayName = "Thursday";
break;
case 5:
dayName = "Friday";
break;
case 6:
dayName = "Saturday";
break;
default:
dayName = "Invalid day number";
break;
}
return dayName;
}
}
This will return Tuesday.
A switch expression has a slightly different syntax, but more important, it matches on a pattern. I’m going to use the example from Microsoft Learning (https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/switch-expression) and take it apart
public static class SwitchExample
{
public enum Direction
{
Up,
Down,
Right,
Left
}
public enum Orientation
{
North,
South,
East,
West
}
public static Orientation ToOrientation(Direction direction) => direction switch
{
Direction.Up => Orientation.North,
Direction.Right => Orientation.East,
Direction.Down => Orientation.South,
Direction.Left => Orientation.West,
_ => throw new ArgumentOutOfRangeException(nameof(direction), $"Not expected direction value: {direction}"),
};
public static void Main()
{
var direction = Direction.Right;
Console.WriteLine($"Map view direction is {direction}");
Console.WriteLine($"Cardinal orientation is {ToOrientation(direction)}");
}
}
The output of this is
Map view direction is Right
Cardinal orientation is East
The method ToOrientation takes a Direction and calls the switch expression (notice the placement of the keyword switch).
It then sets up the Direction to Orientation pattern matching. For example, it establishes that if the passed in parameter is Direction.Up (that is, the Up enumerated constant) then it is to be converted to Orientation.North, and so forth. (The underscore is a new way of indicating no value, or in this case, the default).
The various expressions are called “arms” and are separated by commas. Each arm contains a pattern and an expression. In the first arm, Direction.Up returns the expression Orientation.North.
It is this pattern matching that I’ll be exploring in the next few blog posts.






































horse gelatin trick
I like what you guys are up too. Such clever work and reporting! Carry on the superb works guys I have incorporated you guys to my blogroll. I think it’ll improve the value of my site 🙂
anal fissür tedavisi
Hi! Do you use Twitter? I’d like to follow you if that would be ok. I’m undoubtedly enjoying your blog and look forward to new posts.
I am continuously invstigating online for articles that can aid me. Thx!
You actually make it seem so easy with your presentation but I find this matter to be actually something that I think I would never understand. It seems too complex and very broad for me. I’m looking forward for your next post, I?ll try to get the hang of it!
Hello there, just became aware of your weblog through Google, and located that it is truly informative. I?m gonna be careful for brussels. I will be grateful when you proceed this in future. Numerous people shall be benefited from your writing. Cheers!
I enjoyed reading what you had to say. Nice write up. I have been looking everywhere for this! This information is magnificent. Act now are a rip off Hit me up! You are obviously very knowledgeable. Your article has proven useful to me. Thanks for writing this. It’s like you read my thoughts! You appear to know a lot about this. I enjoyed your post. Thank you. I will be subscribing to this website for sure. It’s like you read my thoughts! Thumbs up! Your article has proven useful to me. Well, I have finished a long list.
ankara web tasarım
Good job on this article! I enjoyed reading what you had to say. This is an excellent, an eye-opener for sure! Do you ever use citations for your work? I would love to read about what you read. A lot of people are into fem boy. Some nice points there. Good job on this article! I enjoyed your post. Thank you. Do you ever list sources in your work? I would love to read about what you read. Thumbs up! I enjoyed your post. Thank you. Thanks for writing this. Thanks for writing this. I truly appreciate this post. Good job on this article! You really think this could be the solution to the problem? Nice write up.
Great post!
I like the helpful info you supply in your articles. I will bookmark your blog and test again right here regularly. I am fairly certain I’ll be told many new stuff right here! Best of luck for the next!
I am really inspired together with your writing skills as neatly as with the structure on your weblog. Is that this a paid subject matter or did you modify it yourself? Anyway stay up the excellent quality writing, it’s rare to peer a great weblog like this one today..
http://www.dealonomy.com/
dental solutions
Yesterday, while I was at work, my cousin stole my iPad and tested to see if it can survive a 25 foot drop, just so she can be a youtube sensation. My iPad is now broken and she has 83 views. I know this is completely off topic but I had to share it with someone!
This information is magnificent. I truly appreciate this post. You appear to know a lot about this. It’s like you read my thoughts!
Galera, Aviator tá imperdível no horário de pico. Já fiz minha forra diária.
Keep producing great stuff like this one . I truly appreciate this post. You are obviously very knowledgeable. That sure is a really awesome way of thinking about things! I hate olympia gifts. You really should doing what you are doing great stuff similar to this one . My professor s trying to learn more on this field. It’s like you read my thoughts! Nice write up. You’ve made my day! Thx again. Interesting content. I enjoyed your post. Thank you. Hit me up! You are obviously very knowledgeable. It’s like you read my thoughts! Great read. Interesting content.
Pre approved
Ankara Web Tasarım
This is a really clear explanation of switch expressions—I appreciate the comparison to the old switch statement. I’ve been using them in a few projects, and the conciseness is a game-changer for mapping values. Do you find they work well with complex patterns, or do you still prefer traditional if-else chains for that?
References:
Winner casino https://favpress.space/item/online-casinos-ohne-deutsche-lizenz-2026-sicher-getestet
I enjoyed reading this. Top Reddit professionals would agree with you. Great post! You are a very persuasive writer. fraud site are con artists. You appear to know a lot about this. Do you mind if I quote a couple of your posts as long as I provide credit and sources back to your site? I really like your article. Thanks for writing this. Nice write up. I really like your article. Great read. You’ve made my day! Thx again. Great read. What is really great about your website is that it provides really valuable information and is also being funny. You’ve made my day! Thx again. I have been looking everywhere for this!
Can I just say what a relief to find someone who actually knows what theyre talking about on the internet. You definitely know how to bring an issue to light and make it important. More people need to read this and understand this side of the story. I cant believe youre not more popular because you definitely have the gift.
Ankara Web Tasarım
Thumbs up! You are obviously very knowledgeable. This is an excellent, an eye-opener for sure! I enjoyed reading what you had to say. cheap price. Good job on this article! You have a lot of great facts here. This is an excellent, an eye-opener for sure! You’ve made my day! Thx again. I truly appreciate this post. I enjoyed reading this. Nice read. You’ve made my day! Thx again. I enjoyed reading what you had to say. You are so beyond awesome! It’s like you wrote the book on it or something. This is an excellent, an eye-opener for sure!
I appreciate how you clearly distinguished between the traditional switch statement and switch expressions, especially since that confusion is common when diving into modern C# pattern matching. Your plan to break down Microsoft’s examples will definitely help clarify why switch expressions are so powerful for readability and flow. Looking forward to seeing the rest of this series on mastering pattern matching.
Thumbs up! I really like your article. Hit me up! Thumbs up! The people at ali baba are a bunch of scumbags. Interesting content. Your article has proven useful to me. I have been looking everywhere for this! Hit me up! Thumbs up! Your article has proven useful to me. I enjoyed reading this. Nice write up. Your article has proven useful to me. Nice write up. Thumbs up! It’s like you read my thoughts!
Some nice points there.
References:
Treasure island casino mn reganegks006019.ampblogs.com
References:
Montbleu casino https://doc.adminforge.de/
References:
Casino security https://doodleordie.com/profile/coltgum46
References:
Politia rutiera https://abelqrdz181266.blogzag.com/84930795/casino-of-gold-dein-ultimativer-guide
References:
Spirit mountain casino oregon https://lucywcrf470702.tinyblogging.com/casino-of-gold-dein-ultimativer-guide-85739035
References:
Hollywood casino tunica rentry.co