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.

Introduction to patterns

Introduction to patterns

- [Instructor] In the next couple of videos, we're going to be focused on patterns. Patterns are used with the is statement, the switch expression, or the switch statement. In fact, we've already seen some. You can see here on line 71, we have the declaration pattern where we've said p is a ShiftWorker, declare this swv as a variable of type ShiftWorker and give it the value that came in as p, but as ShiftWorker. Likewise, when we didn't have that, this is a type pattern. We're saying a particular variable is of a particular type. We'll put that back in there. When I say those patterns can be used in is statements like this or our switch expressions, that means we could rewrite this. So remember, a switch expression is going to return a value, and here we're going to return a string in each of these cases. So if we just said p switch and then we came down here and did that, now I can do ShiftWorker, and I can do something like this where I have an expression. But I want that swv, I…

Contents