How to initialize a dictionary containing lists of dictionaries?

前端 未结 3 1599
一向
一向 2020-12-29 22:01

I am starting to do a little development in C#, and I am stuck with a problem here. Usually I develop in Python where stuff like this is being implemented easily (at least f

3条回答
  •  被撕碎了的回忆
    2020-12-29 22:40

    You can easily do this:

     Dictionary> dict =  
                           new Dictionary>() {
         {"alfred",new Dictionary() {{"age",20.0},{"height":180.1}}},
         {"barbara",new Dictionary() {{"age",18.5},{"height": 167.3}}}
     };
    

    You would be better off using typed person though, or an ExpandoObject to give typed syntax access to the dictionary.

    Dictionary dict = new Dictionary() {
             {"alfred",new Person { age=20.0 ,height=180.1 }},
             {"barbara",new Person { age=18.5,height=167.3 }}
         };
    

提交回复
热议问题