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 think the admin of this web page is in fact working hard in support of his site, since here every stuff is quality based
stuff.
Here is my homepage: apex force reviews
I don’t even know how I ended up here, but I thought this post was great. I do not know who you are but certainly you are going to a famous blogger if you aren’t already 😉 Cheers!
I gotta favorite this website it seems very helpful very beneficial
I just could not depart your site before suggesting that I actually enjoyed the standard information a person provide for your visitors? Is going to be back often to check up on new posts
You actually make it appear so easy with your presentation however I to find this matter to be really one thing which I think I would by no means understand. It sort of feels too complex and extremely extensive for me. I am taking a look ahead on your subsequent publish, I’ll attempt to get the dangle of it!
Hi would you mind letting me know which hosting company you’re utilizing? I’ve loaded your blog in 3 completely different internet browsers and I must say this blog loads a lot quicker then most. Can you recommend a good hosting provider at a reasonable price? Thank you, I appreciate it!
I truly appreciate this post. I have been looking all over for this! Thank goodness I found it on Bing. You have made my day! Thanks again
you are in reality a good webmaster. The site loading velocity is amazing. It kind of feels that you’re doing any unique trick. In addition, The contents are masterwork. you have performed a excellent task in this matter!
Hello, I think your site might be having browser compatibility issues. When I look at your blog in Safari, it looks fine but when opening in Internet Explorer, it has some overlapping. I just wanted to give you a quick heads up! Other then that, wonderful blog!
hi!,I like your writing so much! share we communicate more about your post on AOL? I require an expert on this area to solve my problem. May be that’s you! Looking forward to see you.
As a Newbie, I am always browsing online for articles that can benefit me. Thank you
Saved as a favorite, I really like your blog!
This blog is definitely rather handy since I’m at the moment creating an internet floral website – although I am only starting out therefore it’s really fairly small, nothing like this site. Can link to a few of the posts here as they are quite. Thanks much. Zoey Olsen
I really like your blog.. very nice colors & theme.
Did you design this website yourself or did you hire someone to do it for you?
Plz reply as I’m looking to create my own blog and would like to
know where u got this from. cheers
My web site: sugar harmony reviews
Wow! Thank you! I permanently wanted to write on my website something like that. Can I implement a fragment of your post to my blog?
I am really impressed along with your writing abilities and also with the format to your weblog. Is this a paid subject matter or did you modify it your self? Either way stay up the excellent high quality writing, it’s uncommon to look a great weblog like this one these days..
Great post. I am facing a couple of these problems.
I couldn’t resist commenting
Today, while I was at work, my sister stole my iphone and tested to see if it can survive a 30 foot drop, just so she can be a youtube sensation. My apple ipad is now broken and she has 83 views. I know this is completely off topic but I had to share it with someone!
I was just searching for this information for a while. After 6 hours of continuous Googleing, at last I got it in your web site. I wonder what is the lack of Google strategy that don’t rank this type of informative web sites in top of the list. Usually the top websites are full of garbage.
Hello! I could have sworn I’ve been to this blog before but after browsing through some of the post I realized it’s new to me. Anyways, I’m definitely happy I found it and I’ll be book-marking and checking back frequently!
You are my intake, I own few web logs and very sporadically run out from post :). “No opera plot can be sensible, for people do not sing when they are feeling sensible.” by W. H. Auden.
This is very attention-grabbing, You’re an excessively skilled blogger. I’ve joined your feed and look ahead to in the hunt for extra of your fantastic post. Also, I have shared your web site in my social networks!
Undeniably believe that which you stated. Your favorite justification appeared to be on the web the simplest thing to be aware of. I say to you, I definitely get annoyed while people think about worries that they just don’t know about. You managed to hit the nail upon the top as well as defined out the whole thing without having side-effects , people could take a signal. Will likely be back to get more. Thanks
You actually make it seem so easy along with your presentation however I find this matter to be actually one thing which I feel I’d never understand. It kind of feels too complex and extremely extensive for me. I’m looking ahead on your subsequent submit, I will try to get the hang of it!
Nice post. I learn something more challenging on different blogs everyday. It will always be stimulating to read content from other writers and practice a little something from their store. I’d prefer to use some with the content on my blog whether you don’t mind. Natually I’ll give you a link on your web blog. Thanks for sharing.
hey there and thank you for your info – I have definitely picked up anything new from right here. I did however expertise a few technical points using this site, since I experienced to reload the web site lots of times previous to I could get it to load correctly. I had been wondering if your hosting is OK? Not that I am complaining, but sluggish loading instances times will often affect your placement in google and can damage your high-quality score if advertising and marketing with Adwords. Well I am adding this RSS to my email and could look out for much more of your respective interesting content. Make sure you update this again very soon..
Your style is so unique compared to many other people. Thank you for publishing when you have the opportunity,Guess I will just make this bookmarked.2
Greetings! Very helpful advice on this article! It is the little changes that make the biggest changes. Thanks a lot for sharing!
Há muitas opções.
Hi I am so delighted I found your web site, I really found you by mistake, while I was searching on Bing for something else, Regardless I am here now and would just like to say cheers for a incredible post and a all round thrilling blog (I also love the theme/design), I don’t have time to read through it all at the minute but I have bookmarked it and also added in your RSS feeds, so when I have time I will be back to read more, Please do keep up the fantastic work.
Youre so cool! I dont suppose Ive read something like this before. So good to search out someone with some original thoughts on this subject. realy thanks for beginning this up. this web site is one thing that’s needed on the web, someone with a little bit originality. useful job for bringing something new to the internet!
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!
This website online is mostly a walk-via for the entire information you wished about this and didn’t know who to ask. Glimpse here, and you’ll positively uncover it.
Hi my friend! I want to say that this post is amazing, nice written and include approximately all important infos. I would like to see more posts like this.
Absolutely composed content material, Really enjoyed looking at.
I am impressed with this internet site, very I am a big fan .
Yay google is my king aided me to find this outstanding website ! .
I reckon something really special in this website.
great post, very informative. I’m wondering why the opposite specialists of this sector don’t understand this. You should continue your writing. I am sure, you’ve a huge readers’ base already!
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 soon as you wake up.
My brother recommended I may like this website. He was totally right. This post actually made my day. You can not imagine simply how much time I had spent for this information! Thanks!
As I web-site possessor I believe the content material here is rattling excellent , appreciate it for your efforts. You should keep it up forever! Good Luck.
Hello very nice blog!! Guy .. Beautiful .. Superb .. I will bookmark your website and take the feeds additionally…I’m happy to seek out numerous helpful information right here in the post, we’d like develop more strategies on this regard, thank you for sharing. . . . . .
It’s really a nice and helpful piece of info. I am glad that you shared this useful information with us. Please keep us informed like this. Thanks for sharing.
I like this blog its a master peace ! Glad I detected this on google .
naturally like your website however you need to take a look at the spelling on several of your posts. A number of them are rife with spelling issues and I in finding it very troublesome to tell the truth then again I will surely come back again.
Very interesting topic, regards for posting.
I¦ve learn a few excellent stuff here. Definitely worth bookmarking for revisiting. I wonder how much effort you place to make the sort of excellent informative web site.
I am delighted that I found this website, exactly the right information that I was looking for! .