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.
Using the is expression - C# Tutorial
From the course: C# Essential Training 1: Types and Control Flow
Using the is expression
- We go back down here, notice we did some checks around inputs being null or not null. There're other kinds of checks that we might want to do against say the type. So we're looking at the type and whether or not it's null, but we might want to do something like this. I'm going to paste a couple items in here. Line 62 and 63, I'm defining a shift worker and a manager, but notice that the variable type I'm creating is an I person. I'm saying I have a shift worker that implements I person. So my variable SW is an I person. Likewise, my MGR or manager variable is an I person. And I have a get person details method that takes an I person. In earlier versions of C-sharp, you might do something like you see here on line 68, where you would use the as keyword in order to essentially cast an object. On the left, I'm saying I have a nullable shift worker. Remember our nullable reference types now. So this is a nullable reference type. My shift worker equals P the I person that was passed in…
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)
-