0

val is declared as an object type, and it is assigned a list dictionary .

val object type is {System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, object>>}

I can cast it back to "List<Dictionary<string,object>>"

val as List<Dictionary<string,object>>  is fine , it returns a list of 3 dictionaries , System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, object>>

Dictionary<string,object> is an object type, but why cannot I cast it to "List" ?

val as List<object> == null S>
ValidfroM
  • 2,626
  • 3
  • 30
  • 41
  • 1
    Imagine if the compiler would allow you to do this. What would happen if you added a non-dictionary object to the list `(val as List).Add("hello");` You can convert it using covariant interfaces, eg `val as IReadOnlyList` works, as you can't add items via that interface – Charlieface Jun 13 '23 at 00:41

0 Answers0