language design - Why doesn't C# let me index into dynamic like a dictionary? -


the dynamic keyword in c# let me this:

dynamic obj = ....; var foo = obj.foo; 

where property reference obj.foo resolved @ runtime.

since property resolved @ runtime, why can't specify property variable? example,

var propname = "foo"; var foo = obj[propname]; 

?

i'm aware can accomplish through reflection or converting object dictionary. i'm not interested in solution explanation why c# doesn't support javascript-like square bracket lookup in first place.

one reason why has not been done ambiguity.

consider example:

dynamic d = new dictionary<string,object>() {     {"count", 2} }; object c = d["count"]; 

on 1 hand, dictionary has property count, , dictionary has 1 entry, c should set 1. on other hand, dictionary has entry key "count", c should set 2.

selecting either 1 of these alternatives arbitrarily discard other possibility. in cases best not introduce feature in first place.


Comments