做一个更好的方法来包装类中的多个列表/字典,并循环键和值 [英] Make a better approach to wrap multiple lists/ dictionary inside class and looping key and value
问题描述
我将此代码用于关联数组
I made this code for associative array
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#中感觉非常不直观:将多个列表/字典包含在一起是不是最优雅的解决方案。
After that I thought that This feels very unintuitive in C#: Wrapping multiple Lists/Dictionarys inside each other isn't the most elegant solution.
所以我认为这可能是一个更好的方法来将这个结构包装在类中:
So I made it might be a better approach to wrap this construct inside classes:
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; }
}
我在主要的
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}}}
};
但我有一个例外,我尝试修复它,但我再次收到异常
but i got an exception , i try to fix it but i get an exception again
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:
如何做到这一点我想循环该键和值?
How to do that and i want to loop that key and value ?
推荐答案
NullReference
因为从未创建列表< int>
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
未正确创建:indestead {5,4,5,5}
,使用新列表< int> ...
IntegerValues
is not properly created: indestead of {5,4,5,5}
, use new List<int> ...
您还可以保留语法并使用静态对象初始化器: / p>
You could also keep your syntax and use a static object initializer:
public class DataValue {
public string Name { get; set; }
public List<int> IntegerValues = new List<int>();
}
那么你可以创建 DataValue
像这样:
then you can create DataValue
like this:
new DataValue {Name = "first", IntegerValues = {5,4,5,5}}
循环整数(或其他)这样简单:
Looping over the Integers (or anything else) is as easy as that:
foreach(DataContainer dataContainer in data) {
foreach (int intValue in dataContainer.MyValue.IntegerValues) {
Console.WriteLine(intValue);
}
}
这篇关于做一个更好的方法来包装类中的多个列表/字典,并循环键和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!