I am trying to learn all the new goodies that come with C# 4.0. I am failing to understand the differences between the DynamicObject
and ExpandoObject
types. It seems like DynamicObject
is used e.g. when you want to access variables from Python scripts and ExpandoObject
when talking with COM/Office objects. Am I right? What is the difference in their use?
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.
C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.
Expando is a dynamic
type to which members can be added (or removed) at runtime. dynamic
is designed to allow .NET to interoperate with types when interfacing with dynamic typing languages such as Python and JavaScript.
So, if you need to handle a dynamic type: use dynamic
and if you need to handle dynamic data such as XML or JSON: use ExpandoObject
The declaration of an expando shows the relationship between dynamic and the expando:
dynamic expando = new ExpandoObject();
And the ability to add a new property:
expando.SomeNewStringVal = "Hello World!";
That last line of code creates a brand new string property in the expando object called SomeNewStringVal
. The string type is inferred from the assignment.
So an expando is a dynamic data type that can represent dynamically changing data. That's it in a nutshell. Here's a deeper look at dynamic and expando.
Complete example:
using System; using System.Dynamic; class Program { static void Main(string[] args) { dynamic expando = new ExpandoObject(); expando.SomeNewStringVal = "Hello Brave New Whirrled!"; Console.WriteLine(expando.SomeNewStringVal); // more expando coolness/weirdness: var p = expando as IDictionary<String, object>; p["A"] = "New val 1"; p["B"] = "New val 2"; Console.WriteLine(expando.A); Console.WriteLine(expando.B); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With