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.

Using the dynamic type

Using the dynamic type

- [Instructor] We've seen how to use the dynamic type with a rectangle or an existing type. There are some other ways that you can use the dynamic type. For example, I can use this type called ExpandoObject like I do on line 39. And if you go up, you'll notice I've added on line three, a using statement for System.Dynamic. So that brings that ExpandoObject in scope. And that ExpandoObject can be treated, because I have a dynamic variable, just like I treated the other dynamic object. So on line 40 you can see I can set an area property on it that doesn't exist. This is a ExpandoObject, it doesn't have that property, but I'm creating it by treating it dynamically. So I'm using the area variable, from my previous example, and I'm subtracting 50. And then I can write that out. So I can create an ExpandoObject with a dynamic variable like this, and I can add and set any properties that I want. And then of course I can read…

Contents