Posts

Showing posts with the label JavaScript

C# Dynamic Types & Expando Object

Image
Let me get this straight!! You want to declare an Object but you don't know what member variables you need ahead of time. Have you gone mad? JavaScript Land You've got to love the dynamic nature of JavaScript and how the language allows you to mutate objects at run-time. You don't need to define class definitions of an object upfront. That's so old-skool! You can just create them... on the fly! var movie = new Object(); movie.name = "Interstellar"; movie.genre = "Sci-Fi"; movie.director = "Christopher Nolan"; Later, if you want to add a new member variable or a function at run-time.. Just add it! movie.Rating = 8; And of course you could do the same thing using the object literal notation: var movie2 = { name: "Aliens", genre:"Action", director:"James Cameron" }; and later: movie2.Rating = 9; C# Land In C# land, things are a little different. A bit more traditional. Our objects need to ...