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 - C# Tutorial
From the course: C# Essential Training 1: Types and Control Flow
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…
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)
-