【问题标题】:How to make a collection in c#? [duplicate]如何在 C# 中创建一个集合? [复制]
【发布时间】:2023-03-30 15:59:01
【问题描述】:

我在 PHP 中有一个关联数组

$data=array(
    100=>array(
        "first"=>array(4,24,5,0),
        "second"=>array(42,58,23,8),
        "third"=>array(4,1,14,0),
        "fourth"=>array(49,41,28,2),
        "fifth"=>array(41,30,13,5)
    ),
    500=>array(
        "first"=>array(4,24,5,0),
        "second"=>array(42,58,23,8),
        "third"=>array(4,1,14,0),
        "fourth"=>array(49,41,28,2),
        "fifth"=>array(41,30,13,5)
    )
);



我想将该代码移动到 C# 编程中,但我不知道如何移动它。我尝试使用列表,但我只能接受 100 和 500。
如何将其移入 c# 编程中?

【问题讨论】:

  • 您使用List做出了正确的选择,您在实现中遇到了什么问题?
  • 请显示您在c#中实际尝试过的内容
  • @Ian A List 不是关联数组,那么是什么让您认为它完全合适?
  • @Servy 我宁愿说,List 还可以,我想......也许right 是一个相当强的词(可能是因为缺乏更好的理解)。但我的推理是因为数据集很小,List 易于处理,如果需要也可以将 KVP 放在List 中。也许有更好/更有效的选择超出我的知识范围。但对于给定的情况,我会说List 是可以接受的。
  • 考虑创建代表您尝试使用数组建模的概念的类。

标签: c# php


【解决方案1】:

使用Dictionary 类。

所以你可以这样做:

        var dict = new Dictionary<int, Dictionary<string, int[]>>
        {
            {
                100, new Dictionary<string, int[]>
                {
                    {"first", new[] {4, 24, 5, 0}},
                    {"second", new[] {42, 58, 23, 8}}
                }
            },
            {
                200, new Dictionary<string, int[]>
                {
                    {"first", new[] {4, 24, 5, 0}},
                    {"second", new[] {42, 58, 23, 8}}
                }
            }
        };

【讨论】:

  • 看起来外部字典的键是整数,而不是字符串。并且内部字典的值是一个int数组,而不仅仅是一个int。
  • 这只是一个示例..但我刚刚更新了正确的实现
  • 在 C# 6 中使用新的字典初始化器会更好
【解决方案2】:

列表和字典是要走的路。

首先你应该确保你使用的是通用集合命名空间:

using System.Collections.Generic;

在文件的顶部。

然后您可以像这样创建整数列表字典字典:

var data = new Dictionary<int,Dictionary<string,List<int>>>() {
   {100, new Dictionary<string, List<int>>() {
      {"first", new List<int>() {4, 24, 5, 0}},
      {"second", new List<int>() {42, 58, 23, 8}} //TODO - add third, fourth etc.
   }},
   {500, new Dictionary<string, List<int>>() {
      {"first", new List<int>() {4, 24, 5, 0}},
      {"second", new List<int>() {42, 58, 23, 8}} //TODO - add third, fourth etc.
   }}
}

【讨论】:

    【解决方案3】:
    var data = new Dictionary<int, Dictionary<string, List<int>>>();
    

    关联数组在c#中称为Dictionary

    附带说明:这在 C# 中感觉非常不直观:将多个列表/字典相互包装并不是最优雅的解决方案。


    根据您的要求,将这个构造封装在类中可能是一种更好的方法:

    可能是这样的:

    public class DataContainer {
      public int Index { get; set; }
      public DataValue MyValue { get; set; }
    }
    
    public class DataValue {
      public string Name { get; set; }
      public List<int> IntegerValues { get; set; }
    }
    

    那么你可以简单地拥有一个 DataContainers 列表:var data = new List&lt;DataContainer&gt;();

    【讨论】:

      【解决方案4】:

      PHP 的关联数组映射到C#Dictionary 类。数值数组映射到 List 类,或更简单的 array(例如 int[]、string[])。

      C#PHP 之间的一个主要区别是C# 是一种strongly typed 编程语言,这意味着您需要声明变量的类型。然后,您可以使用文字语法来构建字典。

      var data = new Dictionary<int, Dictionary<string, int[] > >()
      {
          [100] = new Dictionary<string, int[] >() 
          {
              ["first"] = new int[] {4, 24, 5, 0},
              ["second"] = new int[] {42, 58, 23, 8}
              // the rest of the items
          }
          // the rest of the items
      };
      

      注意。感谢@juharr 指出,上述语法适用于C# 6。对于以前的版本,可以使用类似的语法:

      var data = new Dictionary<int, Dictionary<string, int[] > >()
      {
          { 100, new Dictionary<string, int[] >()
              {
                  {"first", new int[] {4, 24, 5, 0} },
                  {"second", new int[] {42, 58, 23, 8} }
                  // the rest of the items
              }
          }
          // the rest of the items
      };
      

      【讨论】:

      • 需要注意的是,这种字典初始化方式需要C# 6。
      • @juharr 感谢您的通知,将更新答案
      【解决方案5】:

      我认为您正在寻找字典:https://msdn.microsoft.com/en-us/library/xfhwa508(v=vs.110).aspx

      这是我认为您发布的内容的一对一映射(您也可以将 List 替换为 int[]):

      注意:我确实看到你的内部字典总是相同的,拥有 2 个内部字典没有意义,但就像我说的,这是我所看到的 1 对 1 映射

                  var mainDictionary = new Dictionary<int, Dictionary<string, List<int>>>();
                  var firstInnerDictionary = new Dictionary<string, List<int>>();
                  var secondInnerDictionary = new Dictionary<string, List<int>>();
      
                  firstInnerDictionary.Add("first", new List<int>{4,24,5,0});
                  mainDictionary.Add(100, firstInnerDictionary);
      
                  secondInnerDictionary.Add("first", new List<int>{4,24,5,0});
                  mainDictionary.Add(500, firstInnerDictionary);
      

      【讨论】:

        【解决方案6】:

        我的建议是使用Dictionary

        var data = new Dictionary<int, Dictionary<string, List<int>>>;
        data[100] =new Dictionary<string, List<int>>();
        data[100]["first"] = new List<int>();
        data[100]["first"].AddRange(new [] {4, 24, 5, 0});
        // etc...
        

        虽然有字典和列表初始化器使代码更紧凑。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-08-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-02-22
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多