Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shorthand version of ExpandoObject or a Dictionary?

Tags:

c#

.net

I really like the anonymous type syntax. e.g.

 new {a = 1, b = 2, c = 25.2}

It'd be neat if there was a version like this for Expando or Dictionary. Is there a way?

like image 996
sgtz Avatar asked Dec 26 '11 06:12

sgtz


2 Answers

You can do this

Dictionary<string, object> dict = 
     new Dictionary<string, object> { { "a", "foo" }, { "b", 1 } };

Also note that you can do

var O = new { A = "foo", B = 12 };
like image 134
Adam Rackis Avatar answered Oct 31 '22 23:10

Adam Rackis


If you could settle for Dictionary, you could try:

var dict = 
    new Dictionary<string, double> { { "a", 1 }, { "b", 2 }, { "c", 25.2 } };
like image 28
Justin Niessner Avatar answered Nov 01 '22 01:11

Justin Niessner