更好地将多个列表/字典包装在类中并循环键和值

时间:2016-01-08 18:11:51

标签: c# list dictionary

我为关联数组

创建了这段代码
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#中感觉非常不直观:将多个列表/ Dictionarys包装在一起并不是最优雅的解决方案。

所以我做了一个更好的方法将这个构造包装在类中:

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: 

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

2 个答案:

答案 0 :(得分:0)

发生NullReference因为永远不会创建List<int>的{​​{1}}。

您可以像这样更改代码:

IntegerValues

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

答案 1 :(得分:0)

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

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

      public DataValue()
      {
          IntegerValues = new List<int>();
      }
    }
​