8 May 2010 - Digital Strategy // By Productive Edge Team

Examining .NET 4.0 with C#: ExpandoObject

Last week, Igor brought up a good point about the usefulness of the standard property syntax as a language construct ( To Bean or not to Bean!). How much real benefit is there in building boilerplate getters and setters into your code? I brought up the C#:
public string Name { get; set; } 

syntactic sugar, it still isn’t as clean or concise as Ruby:

class Person
attr_accessor :first_name, :last_name

or what he envisioned for Java:

//context of another
class Person p = new Person();
p.firstName := "John";

where the getter and setter are automatically appended to the Person object by the compiler.

Well, .NET 4.0 still isn’t quite as cool and concise, but it is getting closer with the introduction of the ExpandoObject. By adding the following using statement:

using System.Dynamic; 

you now get the ability to write code like:

dynamic p = new ExpandoObject();
p.firstName = "John";

Oh, and did I mention that you get IntelliSense with it as well?


IntelliSense with ExpandoObject properties. You still get IntelliSense with ExpandoObject dynamic properties.


When using ExpandoObject(s) with .NET 4.0 and C#, you get IntelliSense for all of the dynamically generated fields.

link: http://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject(v=VS.100).aspx

Examining .NET 4.0 with C#: ExpandoObject