【问题标题】:How to create and populate generic Dictionary? [closed]如何创建和填充通用字典? [关闭]
【发布时间】:2012-08-07 06:20:06
【问题描述】:

如何创建和填充通用Dictionary<> 它应该由这种类型填充 TKey= string, TValue=Point.

提前谢谢你。

【问题讨论】:

    标签: c# .net dictionary


    【解决方案1】:

    怎么样

    Dictionary<string, Point> spdic = new Dictionary<string, Point>();
    
    spdic.Add("mystring", new Point(0,0)); 
    spdic.Add("mystring1", new Point(23,30)); 
    

    【讨论】:

      【解决方案2】:

      就这样

      Dictionary<string,Point> dic = newDictionary<string,Point> ();
      dic.Add("key", new Point(1,1));
      

      【讨论】:

        【解决方案3】:
        Dictionary<string, Point> myDict = new Dictionary<string, Point>();
        myDict.add(myString, myPoint);
        

        http://msdn.microsoft.com/en-us/library/xfhwa508.aspx

        【讨论】:

          【解决方案4】:

          怎么样

          Dictionary<string, Point> dict = new Dictionary<string, Point>()
          {    
              { "string A", new Point(0, 0) }
          }
          

          只是为了与其他答案不同并使用对象初始化器

          【讨论】:

          • new new Point(0, 0) - 拼写错误需要更改
          【解决方案5】:

          你可以试试:

          Dictionary<string, Point> myDictionary = new Dictionary<string, Point>();
          myDictionary.Add("Some String", new Point(x, y)); 
          

          希望这会有所帮助!

          【讨论】:

            【解决方案6】:
            Dictionary<String, Point> dict = new Dictionary<String, Point>();
            
            dict.Add("abcd", new Point(65,99));
            

            这里是documentation

            编辑请确保您阅读文档或进行一些搜索,因为您很可能会找到此类问题的答案。以下是您如何使用Add 方法

            【讨论】:

              【解决方案7】:

              您还可以列出包含字符串和点的任何类型的列表并使用

              listOfThingsWithStringsAndPoints.ToDictionary(x => x.yourString, x=> x.yourPoint);

              只是为了与其他答案不同并使用 ToDictionary

              【讨论】:

                猜你喜欢
                • 2017-12-12
                • 2013-05-12
                • 1970-01-01
                • 2022-01-13
                • 2022-11-25
                • 2021-09-28
                • 1970-01-01
                • 2015-01-23
                • 2020-11-04
                相关资源
                最近更新 更多