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.

Nice post. I learn one thing tougher on totally different blogs everyday. It will all the time be stimulating to read content from other writers and follow a little bit one thing from their store. I’d choose to use some with the content on my weblog whether or not you don’t mind. Natually I’ll provide you with a link in your web blog. Thanks for sharing.
Thank you for sharing with us, I believe this website genuinely stands out : D.
Very clear web site, regards for this post.
F*ckin’ remarkable things here. I am very glad to see your post. Thanks a lot and i’m looking forward to contact you. Will you kindly drop me a mail?
whoah this blog is fantastic i love reading your articles. Keep up the great work! You know, lots of people are searching around for this information, you could help them greatly.
Hey very cool web site!! Man .. Excellent .. Superb .. I’ll bookmark your web site and take the feeds additionally…I am satisfied to find so many helpful information right here within the submit, we want work out extra techniques on this regard, thank you for sharing.
Great wordpress blog here.. It’s hard to find quality writing like yours these days. I really appreciate people like you! take care
so much fantastic info on here, : D.
I?¦m not positive the place you are getting your information, but good topic. I must spend a while studying more or figuring out more. Thank you for wonderful info I was looking for this information for my mission.
I do like the manner in which you have framed this issue plus it does indeed give me personally some fodder for consideration. On the other hand, through just what I have observed, I simply trust as other comments pile on that folks remain on point and not embark on a tirade involving the news du jour. Still, thank you for this excellent point and even though I can not go along with the idea in totality, I respect your perspective.
Hello, i read your blog occasionally and i own a similar one and i was just curious if you get a lot of spam comments? If so how do you stop it, any plugin or anything you can suggest? I get so much lately it’s driving me mad so any help is very much appreciated.
Thankyou for helping out, superb info .
I’ve been absent for a while, but now I remember why I used to love this blog. Thank you, I’ll try and check back more frequently. How frequently you update your website?
Some truly quality content on this site, saved to favorites.
Nice weblog right here! Additionally your website lots up very fast! What host are you using? Can I get your affiliate link for your host? I desire my web site loaded up as fast as yours lol
Keep functioning ,remarkable job!
I reckon something genuinely special in this site.
Everything is very open and very clear explanation of issues. was truly information. Your website is very useful. Thanks for sharing.
Very interesting topic, appreciate it for posting. “What passes for optimism is most often the effect of an intellectual error.” by Raymond Claud Ferdinan Aron.
great put up, very informative. I’m wondering why the opposite experts of this sector do not notice this. You should continue your writing. I am confident, you’ve a great readers’ base already!
You made a number of fine points there. I did a search on the subject matter and found most people will consent with your blog.
Keep up the great piece of work, I read few content on this website and I believe that your blog is rattling interesting and holds circles of great information.
You actually make it appear really easy along with your presentation but I to find this topic to be really something which I believe I’d by no means understand. It seems too complex and very large for me. I am having a look ahead in your subsequent publish, I will try to get the dangle of it!
Nice read, I just passed this onto a friend who was doing a little research on that. And he just bought me lunch since I found it for him smile Thus let me rephrase that: Thanks for lunch!
Very good written story. It will be valuable to everyone who employess it, as well as me. Keep doing what you are doing – i will definitely read more posts.
I am typically to blogging and i actually admire your content. The article has actually peaks my interest. I am going to bookmark your website and hold checking for brand spanking new information.
I truly enjoy looking through on this internet site, it has superb posts.
so much wonderful info on here, : D.
My spouse and i got so fulfilled when John could finish off his studies by way of the precious recommendations he came across through the web site. It is now and again perplexing to just possibly be releasing strategies which usually other folks may have been making money from. So we recognize we now have the website owner to thank because of that. These explanations you’ve made, the simple blog navigation, the friendships you will aid to promote – it is everything amazing, and it’s really leading our son in addition to our family know that that subject matter is cool, and that’s wonderfully indispensable. Thanks for everything!
Hello there I am so delighted I found your weblog, I really found you by mistake, while I was browsing on Bing for something else, Anyways I am here now and would just like to say cheers for a tremendous post and a all round thrilling blog (I also love the theme/design), I don’t have time to read 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 a great deal more, Please do keep up the superb job.
I am often to running a blog and i really admire your content. The article has actually peaks my interest. I’m going to bookmark your web site and hold checking for brand spanking new information.
I am very happy to read this. This is the kind of manual that needs to be given and not the accidental misinformation that is at the other blogs. Appreciate your sharing this best doc.
I like what you guys are up too. Such clever work and reporting! Carry on the excellent works guys I have incorporated you guys to my blogroll. I think it’ll improve the value of my web site 🙂
I was just looking for this info 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 kind of informative web sites in top of the list. Usually the top sites are full of garbage.
Right now it appears like Movable Type is the top blogging platform available right now. (from what I’ve read) Is that what you are using on your blog?
Valuable information. Lucky me I found your site by accident, and I’m shocked why this accident did not happened earlier! I bookmarked it.
Some really nice and utilitarian information on this site, likewise I believe the style and design holds wonderful features.
Well I truly liked reading it. This information offered by you is very practical for proper planning.
It’s a pity you don’t have a donate button! I’d certainly donate to this outstanding blog! I guess for now i’ll settle for book-marking and adding your RSS feed to my Google account. I look forward to fresh updates and will share this blog with my Facebook group. Chat soon!
Fantastic beat ! I would like to apprentice while you amend your web site, how could i subscribe for a weblog website? The account aided me a appropriate deal. I have been tiny bit acquainted of this your broadcast offered shiny transparent idea
Whats Happening i’m new to this, I stumbled upon this I have found It positively helpful and it has helped me out loads. I am hoping to give a contribution & assist different users like its helped me. Great job.
Its excellent as your other posts : D, thanks for putting up. “A gift in season is a double favor to the needy.” by Publilius Syrus.
One more thing I would like to convey is that instead of trying to match all your online degree training on days that you end work (as most people are worn out when they return home), try to receive most of your lessons on the week-ends and only a couple of courses for weekdays, even if it means a little time away from your saturdays. This pays off because on the week-ends, you will be a lot more rested and concentrated with school work. Many thanks for the different recommendations I have discovered from your site.
Very efficiently written story. It will be valuable to anyone who employess it, including yours truly :). Keep up the good work – for sure i will check out more posts.
Some genuinely wonderful work on behalf of the owner of this site, dead outstanding content.
You must participate in a contest for one of the best blogs on the web. I’ll suggest this site!
Thanks for your post here. One thing I would like to say is that most professional areas consider the Bachelor Degree like thejust like the entry level requirement for an online education. When Associate College diplomas are a great way to start, completing a person’s Bachelors starts up many doors to various jobs, there are numerous internet Bachelor Course Programs available through institutions like The University of Phoenix, Intercontinental University Online and Kaplan. Another thing is that many brick and mortar institutions offer you Online variants of their diplomas but typically for a greatly higher price than the firms that specialize in online diploma programs.
Hi, Neat post. There is a problem together with your web site in internet explorer, may test this?K IE nonetheless is the market leader and a huge section of people will leave out your great writing because of this problem.
I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post…
Oh my goodness! a tremendous article dude. Thank you Nonetheless I’m experiencing issue with ur rss . Don’t know why Unable to subscribe to it. Is there anybody getting identical rss problem? Anyone who knows kindly respond. Thnkx