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.
Property pattern - C# Tutorial
From the course: C# Essential Training 1: Types and Control Flow
Property pattern
- Another very useful pattern we can use is a property pattern. I'm going to create a method here. Its going to be a static bool. And we're going to check, "is weekend night shift". We're going to see for a given date and time, is this on a weekend? Friday or Saturday. And does it happen after 3:00 PM? So we're going to pass in the date time, called the shift date and we'll use our expression bodied method here. And I'm going to pass in that shift date to a switch expression. So we'll come down here, close that out. So we've got a method, we've got our switch expression on the shift date. And what I want to do is look at the properties of that date. So notice if I do curly braces here, I get all of the different properties of a date time. So now I could do our hour:, I could say, greater than or equal to 15, so 3:00 PM. And then I do a comma and now I have access to the properties again. So I could say day of week:, and then I can use the day of week, Monday, Friday, or day of week…
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)
-