Javascript "var obj = new Object" Equivalent in C#

PercivalMcGullicuddy picture PercivalMcGullicuddy · Jul 8, 2011 · Viewed 9.2k times · Source

Is there an easy way to create and Object and set properties in C# like you can in Javascript.

Example Javascript:

var obj = new Object;

obj.value = 123476;
obj.description = "this is my new object";
obj.mode = 1;

Answer

Andrew Florko picture Andrew Florko · Jul 8, 2011

try c# anonymous classes

var obj = new { 
    value = 123475, 
    description = "this is my new object", 
    mode = 1 };

there are lots of differences though...

@Valera Kolupaev & @GlennFerrieLive mentioned another approach with dynamic keyword