From the course: C# Essential Training 1: Types and Control Flow
Unlock the full course today
Join today to access over 23,200 courses taught by industry experts.
Guard conditions - C# Tutorial
From the course: C# Essential Training 1: Types and Control Flow
Guard conditions
- [Instructor] In addition to all the great patterns we've looked at, we also have something called guard conditions. For example, with our shift worker here, we might want to indicate that, yes, we want it to be a shift worker, but we might also say "when." And now we have that SWV, we can look at the shift worker. We could look at that and we could say the StartDate.year is greater than 2020. What that does is it puts an additional constraint on this branch of our switch expression. So in addition to this needing to be a shift worker type, the p variable coming in, being a shift worker, we also need the start date to be greater than 2020. Now let's copy this line. We'll paste it, and then we'll say, it's less than or equal to 2020. And here we'll just write out a simple string. We'll say, "older employee." So that might be that they're grandfathered into something, or we want to do something else. On both cases, the p represents a shift worker, but we have some additional condition…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
-
(Locked)
If and switch revisited5m 26s
-
(Locked)
Switch statement vs. switch expression4m 55s
-
(Locked)
Using the is expression5m 22s
-
(Locked)
Introduction to patterns3m 46s
-
(Locked)
Constant, relational, and logical patterns3m 17s
-
(Locked)
Property pattern4m 22s
-
(Locked)
Guard conditions3m 57s
-
(Locked)
Solution: Switch expression using patterns2m 16s
-
(Locked)
-