使用更好的方法将多个列表/字典包装在类和循环键和值中

本文关键字:包装 字典 键和值 循环 列表 更好 方法 | 更新日期: 2023-09-27 18:29:29

我为关联数组编写了这段代码

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.
   }}
}

在那之后,我认为这在C#中感觉非常不直观:将多个Lists/Dictionary封装在一起并不是最优雅的解决方案。

因此,我认为将这个结构封装在类中可能是一种更好的方法:

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

我在主中列出了这样的DataContainer列表

var data = new List<DataContainer>()
            {
                new DataContainer{index = 100 , DataValue = new DataValue
                {name = "first", IntegerValues = {5,4,5,5}}},
                 new DataContainer{index = 100 , DataValue = new DataValue
                {name = "second", IntegerValues = {10,45,5,65}}},
                 new DataContainer{index = 100 , DataValue = new DataValue
                {name = "third", IntegerValues = {10,45,5,65}}}

            };

但我遇到了一个异常,我试图修复它,但我再次遇到异常

System.NullReferenceException was unhandled
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=FalaqHijriyyah
  StackTrace:
       at FalaqHijriyyah.Program.Main(String[] args) in d:'Console'FalaqHijriyyah'FalaqHijriyyah'Program.cs:line 13
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

如何做到这一点,我想循环键和值?

使用更好的方法将多个列表/字典包装在类和循环键和值中

使用{get;set;}时,需要初始化您的List<>构造函数中的对象。

    public class DataValue {
      public string Name { get; set; }
      public List<int> IntegerValues { get; set; }
      public DataValue()
      {
          IntegerValues = new List<int>();
      }
    }
​

NullReference之所以发生,是因为从未创建IntegerValuesList<int>

你可以这样更改你的代码:

  var data = new List<DataContainer>()
        {
            new DataContainer{Index = 100 , MyValue = new DataValue
            {Name = "first", IntegerValues = new List<int>{5,4,5,5}}},
             new DataContainer{Index = 100 , MyValue = new DataValue
            {Name = "second", IntegerValues = new List<int>{10,45,5,65}}},
             new DataContainer{Index = 100 , MyValue = new DataValue
            {Name = "third", IntegerValues = new List<int>{10,45,5,65}}}
        };

IntegerValues未正确创建:代替{5,4,5,5},使用new List<int> ...


您也可以保留语法并使用静态对象初始化器:

public class DataValue {
  public string Name { get; set; }
  public List<int> IntegerValues = new List<int>();
}

然后你可以像这样创建DataValue

new DataValue {Name = "first", IntegerValues = {5,4,5,5}}

在整数(或其他任何东西)上循环就这么容易:

        foreach(DataContainer dataContainer in data) {
            foreach (int intValue in dataContainer.MyValue.IntegerValues) {
                Console.WriteLine(intValue);
            }
        }