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.






































Woah! I’m really enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s challenging to get that “perfect balance” between superb usability and visual appeal. I must say you have done a superb job with this. Also, the blog loads extremely quick for me on Safari. Excellent Blog!
I am always searching online for tips that can aid me. Thank you!
Really enjoyed this update, is there any way I can receive an email when you write a fresh post?
I have been browsing online more than 3 hours these days, yet I by no means discovered any interesting article like yours. It is beautiful value enough for me. Personally, if all web owners and bloggers made excellent content material as you probably did, the internet will be much more helpful than ever before.
Pretty section of content. I just stumbled upon your web site and in accession capital to assert that I acquire in fact enjoyed account your blog posts. Any way I’ll be subscribing to your augment and even I achievement you access consistently rapidly.
F*ckin’ remarkable things here. I’m very glad to see your article. Thanks a lot and i’m looking forward to contact you. Will you kindly drop me a e-mail?
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 answer back as I’m looking to construct my own blog and would like to find out where u got this from. cheers
You are a very capable person!
I like this post, enjoyed this one thanks for putting up. “‘I have done my best.’ That is about all the philosophy of living one needs.” by Lin Yutang.
Hey, you used to write magnificent, but the last few posts have been kinda boring?K I miss your great writings. Past several posts are just a little out of track! come on!
Valuable info. Lucky me I found your site by accident, and I’m shocked why this accident did not happened earlier! I bookmarked it.
We’re a group of volunteers and starting a new scheme in our community. Your website offered us with valuable information to work on. You’ve done a formidable job and our entire community will be thankful to you.
新作スロットは開発されたばかりということで、目新しく期待値が高まりますよね。 更新日: 2025年04月29日 公開日: 2021年06月24日 カジノシークレットでは登録だけで50ドルの入金不要ボーナスが受け取れます。さらに、初回入金時には200%還元で最大500ドル、2回目は100%還元で最大300ドル、3回目は150%還元で最大200ドルのウェルカムボーナスが付与されます。 認証が完了すると当サイト限定の入金不要ボーナスが追加されます! シンボルが5つ揃うとクウォンタムフィーチャーが発生して、右上のチャージメーターが1つ増えます。このメーターが5つ一杯になると、Gargantoon (ガーガントゥーン)がリール上に現れて高額獲得のヘルプをしてくれます。
http://eudat1.deic.dk/user/bernacontbroch1983
オンラインカジノでは日々新作スロットが登場。その多くを手がけているのが「Pragmatic Play」などの有名メーカーたちです。 爆発力よりも安定感のあるスロットだけをピックアップしたので、効率よく入金不要ボーナスを消化できます。 ベラジョンカジノでプレイできるスロットにはジャックポットという大当たりがあり、他のゲームでは比べ物にならない程の高配当が獲得できる可能性を持っていることが特徴です。 シンボルの並びや後述するフィーチャーが噛み合えば、通常時でも簡単に100倍近い配当が取れるのが、リアクトゥーンズの魅力の1つです。 最小出金額は20ドル、ただし銀行送金は500ドルです。1度にできる最大出金額は10,000ドルなので、ハイローラーの方も十分の額でしょう。
Normally I do not read post on blogs, however I wish to say that this write-up very compelled me to take a look at and do it! Your writing taste has been surprised me. Thank you, quite great post.
Thanks , I’ve recently been looking for info about this subject for ages and yours is the best I’ve discovered till now. But, what about the conclusion? Are you sure about the source?
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!
Thank you for sharing with us, I believe this website truly stands out : D.
Good day! Would you mind if I share your blog with my zynga group? There’s a lot of people that I think would really enjoy your content. Please let me know. Thanks
I simply couldn’t go away your site before suggesting that I extremely loved the usual information a person supply in your guests? Is going to be back frequently in order to check out new posts.
I am no longer positive the place you are getting your info, however great topic. I must spend some time studying much more or understanding more. Thanks for fantastic information I used to be in search of this information for my mission.
Appreciating the hard work you put into your blog and in depth information you provide. It’s great to come across a blog every once in a while that isn’t the same unwanted rehashed information. Fantastic read! I’ve bookmarked your site and I’m adding your RSS feeds to my Google account.
Keep working ,great job!
I like what you guys are up also. Such clever work and reporting! Keep up the excellent works guys I¦ve incorporated you guys to my blogroll. I think it’ll improve the value of my web site 🙂
I have not checked in here for a while as I thought it was getting boring, but the last several posts are good quality so I guess I?ll add you back to my everyday bloglist. You deserve it my friend 🙂
Great write-up, I am normal visitor of one’s website, maintain up the nice operate, and It is going to be a regular visitor for a lengthy time.
Wonderful beat ! I would like to apprentice whilst you amend your site, how could i subscribe for a weblog site? The account aided me a applicable deal. I had been tiny bit acquainted of this your broadcast provided vibrant transparent idea
Hi there, I discovered your web site by the use of Google at the same time as looking for a comparable topic, your website got here up, it appears great. I have bookmarked it in my google bookmarks.
I like what you guys are up too. Such smart work and reporting! Carry on the superb works guys I’ve incorporated you guys to my blogroll. I think it’ll improve the value of my website :).
Whats up very cool website!! Guy .. Excellent .. Wonderful .. I’ll bookmark your site and take the feeds also…I’m satisfied to find a lot of helpful information here in the submit, we’d like work out extra strategies in this regard, thanks for sharing.
Dead indited content material, regards for entropy.
Simply wish to say your article is as surprising. The clarity in your post is simply spectacular and i can assume you are an expert on this subject. Well with your permission let me to grab your RSS feed to keep updated with forthcoming post. Thanks a million and please keep up the enjoyable work.
Very interesting points you have remarked, thanks for putting up.
It’s really a nice and helpful piece of info. I am glad that you shared this helpful information with us. Please keep us informed like this. Thanks for sharing.
Howdy! I know this is kinda off topic but I was wondering which blog platform are you using for this site? I’m getting tired of WordPress because I’ve had problems with hackers and I’m looking at alternatives for another platform. I would be great if you could point me in the direction of a good platform.
I love what you guys are up too. This kind of clever work and coverage! Keep up the excellent works guys I’ve incorporated you guys to blogroll.
My spouse and I absolutely love your blog and find most of your post’s to be just what I’m looking for. Does one offer guest writers to write content to suit your needs? I wouldn’t mind producing a post or elaborating on a few of the subjects you write concerning here. Again, awesome blog!
We are a gaggle of volunteers and starting a brand new scheme in our community. Your web site offered us with useful information to work on. You have performed a formidable task and our whole neighborhood can be grateful to you.
You need to take part in a contest for top-of-the-line blogs on the web. I’ll suggest this website!
I have learned new things out of your blog post. Yet another thing to I have found is that in most cases, FSBO sellers will reject an individual. Remember, they would prefer not to use your expert services. But if an individual maintain a reliable, professional partnership, offering help and keeping contact for four to five weeks, you will usually have the capacity to win interviews. From there, a house listing follows. Thanks
Someone essentially lend a hand to make significantly articles I might state. This is the very first time I frequented your website page and thus far? I amazed with the analysis you made to make this particular submit amazing. Wonderful task!
An interesting discussion is worth comment. I think that you should write more on this topic, it might not be a taboo subject but generally people are not enough to speak on such topics. To the next. Cheers
I like this post, enjoyed this one regards for posting. “We seldom attribute common sense except to those who agree with us.” by La Rochefoucauld.
The following time I read a weblog, I hope that it doesnt disappoint me as much as this one. I imply, I do know it was my choice to learn, however I actually thought youd have something interesting to say. All I hear is a bunch of whining about one thing that you can fix should you werent too busy in search of attention.
I saw a lot of website but I conceive this one contains something extra in it in it
There is apparently a bunch to identify about this. I assume you made various nice points in features also.
I would like to thank you for the efforts you have put in writing this site. I am hoping the same high-grade web site post from you in the upcoming as well. In fact your creative writing skills has inspired me to get my own site now. Actually the blogging is spreading its wings fast. Your write up is a great example of it.
Hello just wanted to give you a quick heads up. The text in your article seem to be running off the screen in Internet explorer. I’m not sure if this is a formatting issue or something to do with internet browser compatibility but I thought I’d post to let you know. The design look great though! Hope you get the issue fixed soon. Many thanks
Along with everything which appears to be building throughout this area, your points of view tend to be fairly refreshing. Nevertheless, I beg your pardon, because I can not subscribe to your whole strategy, all be it exhilarating none the less. It appears to me that your comments are generally not totally validated and in actuality you are your self not even completely certain of the assertion. In any case I did enjoy examining it.
My brother recommended I might like this blog. He was totally right. This publish actually made my day. You can not consider just how so much time I had spent for this information! Thanks!
I want looking at and I think this website got some truly utilitarian stuff on it! .