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 dynamic variables - C# Tutorial
From the course: C# Essential Training 1: Types and Control Flow
Using dynamic variables
- [Illustrator] Well, Var gives you that flexibility to use the anonymous types, where we know a particular structure. I want to go to another type of variable declaration. Say a dynamic D equals new ShiftWorker, we'll go ahead and create that, we'll initialize it. So we'll do FirstName equal Dynamic. I'm going to make it a Dynamic Worker. So LastName equals Worker. Now, if I go to D dot nothing happens, that's because well Var inferred the type from that statement, a dynamic object doesn't assume any particular type. So I can come in and I can use, D dot FirstName here. I can use D kids here. You'll notice I don't have kids defined on this particular ShiftWorker type, but the compilers not complaining about that. Because my D variable is dynamic. And essentially what that allows me to do, is tell the compiler I know what I'm doing. Don't check any of the things I'm doing with his D variable at compile time. Don't make sure it has a property, or method that I'm calling at compile…
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.