I stumbled upon the following and I'm wondering why it didn't raise a syntax error.
var dict = new Dictionary<string, object> { ["Id"] = Guid.NewGuid(), ["Tribes"] = new List<int> { 4, 5 }, ["MyA"] = new Dictionary<string, object> { ["Name"] = "Solo", ["Points"] = 88 } ["OtherAs"] = new List<Dictionary<string, object>> { new Dictionary<string, object> { ["Points"] = 1999 } } };
Notice that the "," is missing between the "MyA", and "OtherAs".
This is where the confusion happens:
Why isn't this illegal? Is this by design?
In C you do not have the this keyword. Only in C++ and in a class, so your code is C and you use your this variable as a local method parameter, where you access the array struct.
Logical OR operator: || The logical OR operator ( || ) returns the boolean value true if either or both operands is true and returns false otherwise.
The missing comma makes all the difference. It causes the indexer ["OtherAs"]
to be applied on this dictionary:
new Dictionary<string, object> { ["Name"] = "Solo", ["Points"] = 88 }
So essentially you're saying:
new Dictionary<string, object> { ["Name"] = "Solo", ["Points"] = 88 }["OtherAs"] = new List<Dictionary<string, object>> { new Dictionary<string, object> { ["Points"] = 1999 } };
Note that this is an assignment expression (x = y
). Here x
is dictionary with "Name" and "Points", indexed with "OtherAs"
and y
is the List<Dictionary<string, object>>
. An assignment expression evaluates to the value being assigned (y
), which is the list of dictionaries.
The result of this whole expression is then assigned to the key "MyA", which is why "MyA" has the list of dictionaries.
You can confirm that this is what's happening by changing the type of the dictionary x
:
new Dictionary<int, object> { [1] = "Solo", [2] = 88 } // compiler error saying "can't convert string to int" // so indeed this indexer is applied to the previous dictionary ["OtherAs"] = new List<Dictionary<string, object>> { new Dictionary<string, object> { ["Points"] = 1999 } }
Here is your code, but reformatted and some parentheses added to illustrated how the compiler has parsed it:
["MyA"] = ( ( new Dictionary<string, object> { ["Name"] = "Solo", ["Points"] = 88 }["OtherAs"] ) = ( new List<Dictionary<string, object>> { new Dictionary<string, object> { ["Points"] = 1999 } } ) )
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