Microsoft has a wonderful tutorial on pattern matching, in which you model a lock (to raise or lower a ship when there would otherwise be a waterfall). They model the two doors and the water level.
While their example is excellent it is long, and in this blog post I’m going to take the essence of pattern matching using their example.
We start with a state machine:

The chart indicates whether the gate should be opened or closed based on the new setting and the current status of the gate as well as the water level. We read it as follows: if the new setting is closed, it doesn’t matter what the starting state of the gate is, nor the water level, we close the gate (first three lines).
On the other hand, if the gate is closed (line 4) and we give it a new state of open and the water level is high, then we open the gate. If, on line 5, we tell it to open from a closed state, but the water level is low then we have an error (don’t open when the water is low).
Finally, if we say open and it is open and the water level is high, then we do, in fact, open.
These states can be modeled in a switch expression using pattern matching where true = open and false = closed:
HighWaterGateOpen = (open, HighWaterGateOpen, CanalLockWaterLevel) switch
{
(false, false, WaterLevel.High) => false,
(false, false, WaterLevel.Low) => false,
(false, true, WaterLevel.High) => false,
(true, false, WaterLevel.High) => true,
(true, false, WaterLevel.Low) => throw new InvalidOperationException("Cannot open high gate when the water is low"),
(true, true, WaterLevel.High) => true,
As you can see, the first three arms of this expression start false and end false, as we saw above. Next, we have the case where the new state is true, the current state is false and the Water level is high. This causes the gate to open (true).
But it gets easier. First, we need the default case. In the latest C# an underscore matches anything. So we add
_ => throw new InvalidOperationException(“Invalid internal state”),
as the default, meaning if anything else comes up, it is invalid.
Note that, as we said, the first three arms all evaluate to false. We can replace them with
(false, _, _) => false,
You read this as, if the first value is false (the new condition) then no matter what the second and third conditions are, we will evaluate to false.
Next we need to know what to do if the new state is true. This is slightly tricker as it depends on the water level:
(true, _, WaterLevel.High) => true,
(true, false, WaterLevel.Low) => throw new InvalidOperationException("Cannot open high gate when the water is low"), => throw new InvalidOperationException("Invalid internal state"),
Thus, if the new state is true, and the water level is high (no matter the state of the gate), then we do open the gate. If the new state is open (from on original state o closed) and the water level is low then, uh oh.
Here is the final version of the method:
// Change the upper gate.
public void SetHighGate(bool open)
{
HighWaterGateOpen = (open, HighWaterGateOpen, CanalLockWaterLevel) switch
{
(false, _, _) => false,
(true, _, WaterLevel.High) => true,
(true, false, WaterLevel.Low) => throw new InvalidOperationException("Cannot open high gate when the water is low"),
_ => throw new InvalidOperationException("Invalid internal state"),
};
}
All of this is simpler, cleaner and thus easier to understand and maintain than a series of if statements or even a set of switch statements.






































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.
I loved as much as you will receive carried out right here. The sketch is attractive, your authored material stylish. nonetheless, you command get bought an shakiness over that you wish be delivering the following. unwell unquestionably come more formerly again since exactly the same nearly very often inside case you shield this hike.
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.
What’s Happening i am new to this, I stumbled upon this I’ve found It absolutely helpful and it has aided me out loads. I hope to contribute & assist other users like its helped me. Good job.
Please let me know if you’re looking for a article author for your weblog. You have some really good posts and I feel I would be a good asset. If you ever want to take some of the load off, I’d absolutely love to write some material for your blog in exchange for a link back to mine. Please send me an email if interested. Thanks!
Only wanna input that you have a very decent site, I like the layout it really stands out.
It’s best to participate in a contest for one of the best blogs on the web. I will advocate this site!
Thanks for your personal marvelous posting! I seriously enjoyed reading it, you will be a great author.I will always bookmark your blog and will come back someday. I want to encourage you continue your great writing, have a nice evening!
I like what you guys are up also. Such intelligent work and reporting! Keep up the excellent works guys I’ve incorporated you guys to my blogroll. I think it will improve the value of my site 🙂
Nice blog here! Also your web site loads up very fast! What host are you the use of? Can I am getting your affiliate hyperlink in your host? I desire my website loaded up as fast as yours lol
Hi there, I found your blog via Google while searching for a related topic, your web site came up, it looks great. I’ve bookmarked it in my google bookmarks.
I was very pleased to find this web-site.I wished to thanks to your time for this glorious read!! I definitely enjoying each little little bit of it and I’ve you bookmarked to take a look at new stuff you blog post.
This is really interesting, You’re a very skilled blogger. I’ve joined your feed and look forward to seeking more of your excellent post. Also, I’ve shared your site in my social networks!
Rattling excellent visual appeal on this web site, I’d value it 10 10.
Hiya, I am really glad I have found this information. Nowadays bloggers publish only about gossips and internet and this is actually annoying. A good blog with exciting content, this is what I need. Thank you for keeping this web-site, I will be visiting it. Do you do newsletters? Can not find it.
Hi there just wanted to give you a quick heads up. The text in your content seem to be running off the screen in Ie. I’m not sure if this is a format issue or something to do with browser compatibility but I thought I’d post to let you know. The layout look great though! Hope you get the issue solved soon. Kudos
Magnificent items from you, man. I have take note your stuff prior to and you are just extremely great. I really like what you’ve got here, certainly like what you’re saying and the best way by which you say it. You’re making it enjoyable and you still take care of to keep it sensible. I can not wait to learn much more from you. That is actually a terrific website.
I will right away grasp your rss feed as I can’t in finding your e-mail subscription hyperlink or newsletter service. Do you have any? Please allow me realize in order that I may just subscribe. Thanks.
悅刻電子菸六代煙彈全面解析
Great post, you have pointed out some great points, I besides believe this s a very wonderful website.
What i don’t realize is actually how you are now not actually much more neatly-favored than you may be now. You’re very intelligent. You know therefore significantly in relation to this matter, made me in my opinion believe it from numerous varied angles. Its like men and women are not interested unless it is something to do with Woman gaga! Your own stuffs nice. At all times maintain it up!
Great goods from you, man. I have understand your stuff previous to and you are just extremely excellent. I actually like what you have acquired here, certainly like what you are stating and the way in which you say it. You make it entertaining and you still take care of to keep it smart. I cant wait to read much more from you. This is really a terrific site.
I like reading and I think this website got some truly utilitarian stuff on it! .
Howdy I am so excited I found your weblog, I really found you by accident, while I was searching on Askjeeve for something else, Regardless I am here now and would just like to say thanks a lot for a incredible post and a all round interesting blog (I also love the theme/design), I don’t have time to read it all at the moment but I have book-marked it and also included your RSS feeds, so when I have time I will be back to read much more, Please do keep up the awesome work.
Thanks for the marvelous posting! I truly enjoyed reading it, you may be a great author.I will be sure to bookmark your blog and will come back in the foreseeable future. I want to encourage you continue your great posts, have a nice afternoon!
You are my inhalation, I possess few blogs and often run out from to post : (.
Thanks , I have recently been looking for information about this subject for ages and yours is the greatest I have discovered till now. But, what about the bottom line? Are you sure about the source?
You got a very superb website, Gladiolus I noticed it through yahoo.
Nearly all of what you say is astonishingly legitimate and it makes me ponder why I had not looked at this in this light before. This particular piece truly did switch the light on for me personally as far as this issue goes. However at this time there is actually one particular position I am not really too cozy with and whilst I try to reconcile that with the actual core idea of the position, let me observe just what all the rest of your readers have to point out.Nicely done.
Good day very nice website!! Man .. Beautiful .. Wonderful .. I will bookmark your site and take the feeds additionally…I am satisfied to find numerous helpful information here within the submit, we need develop more techniques on this regard, thank you for sharing.
Hi my family member! I want to say that this post is awesome, great written and include approximately all important infos. I would like to peer extra posts like this .
Magnificent items from you, man. I have have in mind your stuff prior to and you are just extremely great. I really like what you have acquired right here, really like what you are saying and the way during which you are saying it. You are making it entertaining and you continue to take care of to keep it wise. I can not wait to read far more from you. This is actually a terrific site.
A lot of whatever you say happens to be astonishingly precise and that makes me wonder the reason why I hadn’t looked at this with this light before. This piece truly did turn the light on for me personally as far as this specific subject goes. Nonetheless at this time there is one particular factor I am not necessarily too cozy with so while I attempt to reconcile that with the central theme of your point, permit me see what the rest of the subscribers have to say.Well done.
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.
I have read some good stuff here. Definitely worth bookmarking for revisiting. I surprise how much effort you put to make such a wonderful informative website.
Great website! I am loving it!! Will be back later to read some more. I am bookmarking your feeds also
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.
I like your writing style really enjoying this site.
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.
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.
Along with every thing which seems to be developing within this specific area, many of your opinions are actually somewhat radical. Even so, I am sorry, because I do not subscribe to your whole idea, all be it exciting none the less. It looks to me that your comments are not totally rationalized and in fact you are your self not completely confident of your argument. In any event I did enjoy reading through it.
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.
Hiya, I’m really glad I’ve found this information. Today bloggers publish just about gossips and web and this is really frustrating. A good website with exciting content, that’s what I need. Thank you for keeping this web-site, I will be visiting it. Do you do newsletters? Cant find it.
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.
I like this weblog its a master peace ! Glad I discovered this on google .
Valuable information. Lucky me I found your site by accident, and I’m shocked why this accident did not happened earlier! I bookmarked it.
Hi there, just changed into aware of your blog thru Google, and found that it is truly informative. I am gonna be careful for brussels. I will appreciate if you happen to proceed this in future. A lot of other people might be benefited from your writing. Cheers!
Wonderful work! This is the type of info that should be shared around the web. Shame on the search engines for not positioning this post higher! Come on over and visit my site . Thanks =)
Thank you for sharing superb informations. Your website is very cool. I am impressed by the details that you¦ve on this web site. It reveals how nicely you understand this subject. Bookmarked this web page, will come back for extra articles. You, my pal, ROCK! I found just the information I already searched everywhere and just couldn’t come across. What a perfect site.
Its fantastic as your other blog posts : D, appreciate it for posting. “Music is the soul of language.” by Max Heindel.