From the course: Advanced C#: Hands-on with LINQ, Dynamic Type, Extension Methods, and Tuples
Unlock this course with a free trial
Join today to access over 25,500 courses taught by industry experts.
Solution: Using a dynamic type in a method - C# Tutorial
From the course: Advanced C#: Hands-on with LINQ, Dynamic Type, Extension Methods, and Tuples
Solution: Using a dynamic type in a method
- [Instructor] Let's take a look at a solution to a working with dynamics. So we're going to get this compute output using dynamic call, it's going to get passed in an object and a starting value and we're supposed to return the result of calling a method on there. So we're supposed to use something like this and invoke dynamic method passing in the starting value. That's not going to be happy at first because that parameter type is an object. So I need to change this to a dynamic and now the compiler's going to be happy because it's not going to check it at compile time, it's going to check it at runtime. So our test code, we have a starting value of eight, it's going to invoke our code. If we run that through we've got a correct answer and the code returned 512. If we change this to say 85, we can test again and we've still got the right answer there. Our result matches that. So the key is to switch to the dynamic object…
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.