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.

I’ve been browsing online more than 3 hours as of late, but I never found any attention-grabbing article like yours. It’s beautiful value sufficient for me. In my opinion, if all web owners and bloggers made just right content as you probably did, the internet will be much more helpful than ever before. “When the heart speaks, the mind finds it indecent to object.” by Milan Kundera.
I do not even know how I ended up here, but I thought this post was good. I do not know who you are but definitely you’re going to a famous blogger if you are not already 😉 Cheers!
Saved as a favorite, I really like your blog!
I truly appreciate this post. I have been looking everywhere for this! Thank goodness I found it on Bing. You’ve made my day! Thanks again
Great blog right here! Also your web site so much up fast! What web host are you the usage of? Can I am getting your associate hyperlink in your host? I wish my site loaded up as fast as yours lol
It’s perfect time to make a few plans for the long run and it’s time to be happy. I’ve learn this put up and if I could I wish to suggest you few attention-grabbing issues or tips. Maybe you can write next articles referring to this article. I desire to learn even more issues approximately it!
I really enjoy reading on this internet site, it has got fantastic posts.
It’s really a nice and helpful piece of info. I’m glad that you shared this helpful info with us. Please keep us informed like this. Thanks for sharing.
There is obviously a lot to know about this. I assume you made some good points in features also.
I was suggested this website by way of my cousin. I am no longer positive whether or not this publish is written through him as nobody else recognize such certain approximately my problem. You are amazing! Thanks!
I like what you guys are up too. Such clever work and reporting! Keep up the excellent works guys I have incorporated you guys to my blogroll. I think it will improve the value of my web site 🙂
I have been checking out a few of your posts and it’s pretty good stuff. I will definitely bookmark your site.
Hi there, You have performed a great job. I will definitely digg it and personally suggest to my friends. I’m sure they’ll be benefited from this site.
F*ckin’ amazing things here. I’m very glad to see your post. Thanks a lot and i’m looking forward to contact you. Will you please drop me a mail?
Have you ever considered about adding a little bit more than just your articles? I mean, what you say is valuable and all. But think of if you added some great images or video clips to give your posts more, “pop”! Your content is excellent but with images and clips, this site could certainly be one of the most beneficial in its field. Terrific blog!
Fantastic website you have here but I was curious about if you knew of any community forums that cover the same topics discussed here? I’d really love to be a part of online community where I can get feed-back from other experienced individuals that share the same interest. If you have any suggestions, please let me know. Kudos!
you have got a great blog here! would you wish to make some invite posts on my blog?
Simply wish to say your article is as astounding. The clarity on your publish is just nice and i could suppose you’re knowledgeable in this subject. Fine together with your permission allow me to seize your feed to keep up to date with drawing close post. Thank you a million and please keep up the gratifying work.
Undeniably believe that that you said. Your favourite justification seemed to be on the web the easiest factor to remember of. I say to you, I certainly get irked even as other people consider concerns that they just don’t recognise about. You managed to hit the nail upon the top as well as outlined out the whole thing without having side-effects , folks could take a signal. Will probably be again to get more. Thanks
You are a very intelligent individual!
Very good written post. It will be valuable to anybody who usess it, including myself. Keep doing what you are doing – can’r wait to read more posts.
Aw, this was a really nice post. In idea I want to put in writing like this moreover – taking time and actual effort to make a very good article… however what can I say… I procrastinate alot and in no way seem to get one thing done.
As I website owner I think the content here is very superb, regards for your efforts.
You are a very bright person!
Sweet blog! I found it while searching on Yahoo News. Do you have any tips on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Thank you
Excellent beat ! I wish to apprentice whilst you amend your web site, how can i subscribe for a blog web site? The account aided me a appropriate deal. I had been tiny bit acquainted of this your broadcast offered vivid clear idea
I cherished up to you’ll receive performed right here. The caricature is tasteful, your authored subject matter stylish. however, you command get bought an nervousness over that you want be turning in the following. unwell surely come more until now once more since precisely the similar just about very regularly within case you shield this hike.
Loving the information on this site, you have done outstanding job on the content.
I like this site very much, Its a real nice position to read and incur info .
Aw, this was a very nice post. In concept I wish to put in writing like this additionally – taking time and actual effort to make an excellent article… but what can I say… I procrastinate alot and in no way seem to get something done.
Hello! I just would like to give a huge thumbs up for the great info you have here on this post. I will be coming back to your blog for more soon.
Yeah bookmaking this wasn’t a speculative determination great post! .
I went over this site and I conceive you have a lot of good information, saved to fav (:.
Wow that was strange. I just wrote an very long comment but after I clicked submit my comment didn’t show up. Grrrr… well I’m not writing all that over again. Anyway, just wanted to say great blog!
Very interesting information!Perfect just what I was looking for!
I really like your writing style, superb information, regards for putting up :D. “Your central self is totally untouched By grief, confusion, desperation.” by Vernon Howard.
Hi, i read your blog from time to time and i own a similar one and i was just curious if you get a lot of spam feedback? If so how do you protect against it, any plugin or anything you can advise? I get so much lately it’s driving me mad so any support is very much appreciated.
The Pink Salt Trick is a minimalist but effective morning routine: Just drink a glass of lukewarm water mixed with a pinch of Himalayan pink salt as soo
Hi, i think that i saw you visited my blog thus i came to “return the favor”.I am attempting to find things to enhance my web site!I suppose its ok to use a few of your ideas!!
The Pink Salt Trick is a minimalist but effective morning routine: Just drink a glass of lukewarm water mixed with a pinch of Himalayan pink salt as soo
I got good info from your blog
Hi there this is kinda of off topic but I was wondering if blogs use WYSIWYG editors or if you have to manually code with HTML. I’m starting a blog soon but have no coding expertise so I wanted to get advice from someone with experience. Any help would be greatly appreciated!
The Pink Salt Trick is a minimalist but effective morning routine: Just drink a glass of lukewarm water mixed with a pinch of Himalayan pink salt as soo
This is the right blog for anyone who wants to find out about this topic. You realize so much its almost hard to argue with you (not that I actually would want…HaHa). You definitely put a new spin on a topic thats been written about for years. Great stuff, just great!
Thankyou for this post, I am a big fan of this internet site would like to keep updated.
The Pink Salt Trick is a minimalist but effective morning routine: Just drink a glass of lukewarm water mixed with a pinch of Himalayan pink salt as soo
You made some good points there. I did a search on the subject matter and found most guys will agree with your website.
The Pink Salt Trick is a minimalist but effective morning routine: Just drink a glass of lukewarm water mixed with a pinch of Himalayan pink salt as soo
Useful info. Lucky me I found your website by chance, and I am stunned why this coincidence did not took place earlier! I bookmarked it.
Woh I like your blog posts, bookmarked! .