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

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…

Contents