From the course: C# Essential Training 1: Types and Control Flow
Unlock this course with a free trial
Join today to access over 25,500 courses taught by industry experts.
Solution: Dynamic keyword - C# Tutorial
From the course: C# Essential Training 1: Types and Control Flow
Solution: Dynamic keyword
- [Instructor] In order to return a value, we want to be able to use that answerGenerator that gets passed into us, that's the object that has the method. So we want to be able to simply call that, answerGenerator.GenerateAnswer. We want to pass in the number and that is really the solution. But we can't do that right now because the answerGenerator is an object and the object doesn't have a definition for GenerateAnswer. We can change the type parameter here to say this is a dynamic, essentially that gives us the ability to say, I know this object coming in has some particular properties or methods, and I want it to be dynamic so that I can invoke those. And this tells the compiler, go ahead and let me put this in, GenerateAnswer, even though you're not sure that exists. And at runtime, we'll find out if that method exists. So now we've got the expected result, our code returned 98, which is apparently what we…
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.