简体   繁体   English

在C#中创建结构的数组列表

[英]Create an arraylist of struct in C#

I have a struct 我有一个结构

struct myStruct {
    Dictionary<string, int> a;
    Dictionary<string, string> b;
    ......
}

I want to create a arraylist of that struct 我想创建该结构的arraylist

ArrayList l = new ArrayList();
myStruct s;

s.a.Add("id",1);
s.b.Add("name","Tim");

l.Add(s);

However, I got the error "Object reference not set to an instance of an object." 但是,出现错误“对象引用未设置为对象的实例”。

Anyone could tell me why? 谁能告诉我为什么?

Thanks. 谢谢。

Since your declaration of dictionary a doesn't instantiate it, you are trying to add an item to a null. 由于字典的声明a不会实例化它,因此您试图将一个项目添加到null中。 This is assuming you marked them public, otherwise that wouldn't compile. 这是假设您将其标记为公开,否则将无法编译。

Some suggestions to improve your code: 一些改善代码的建议:

  • Don't use a struct , use a class instead. 不要使用struct ,而要使用class Structs in .NET are a little different and unless one understands those differences I doubt one will ever have a valid use for structs. .NET中结构略有不同 ,除非有人了解这些差异 ,否则我怀疑有人会有效使用结构。 A class is almost always what you want. class几乎总是您想要的。

  • ArrayList is more or less obsolete , it's almost always better to use a generic List<T> instead. ArrayList 或多或少已经过时了 ,最好改用通用的List<T> Even if you need to place mixed objects in the list, List<object> is a better choice than ArrayList . 即使您需要在列表中放置混合对象,与ArrayList相比, List<object>是更好的选择。

  • Make sure your members are properly initialized and not null before you access methods or properties of them. 在访问成员的方法或属性之前,请确保成员已正确初始化且不为null

  • It is better to use properties instead of public fields. 最好使用属性而不是公共字段。

Here is an example: 这是一个例子:

class Container
{
    Dictionary<string, int> A { get; set; }
    Dictionary<string, string> B { get; set; }

    public Container()
    {
         // initialize the dictionaries so they are not null
         // this can also be done at another place 
         // do it wherever it makes sense
         this.A = new Dictionary<string, int>();
         this.B = new Dictionary<string, string>();
    }
}

...
List<Container> l = new List<Container>();
Container c = new Container();
c.A.Add("id", 1);
c.B.Add("name", "Tim");

l.Add(c);
...

The problem is that neither a nor b are initiated. 问题在于ab都不会启动。 Set them to a new dictionary each. 将它们分别设置为新字典。

Edit per comment: 按评论编辑:

Then your problem is elsewhere, as the following works fine: 然后您的问题就在其他地方,因为以下工作正常:

struct myStruct
{
    public IDictionary<string, int> a;
    public IDictionary<string, string> b;
}

IList<myStruct> l = new List<myStruct>();
myStruct s;

s.a = new Dictionary<string, int>();
s.b = new Dictionary<string, string>();
s.a.Add("id", 1);
s.b.Add("name","Tim");

l.Add(s);
 struct myStruct {
    private Dictionary<string, int> a;
    private Dictionary<string, string> b;

    public Dictionary<string, int> A
    {
        get { return a ?? (a = new Dictionary<string, int>()); }
    }

    public Dictionary<string, string> B
    {
        get { return b ?? (b = new Dictionary<string, string>()); }
    }
}

This would solve your problem. 这样可以解决您的问题。 What you need to do, is to access the dictionaries through the properties (getters). 您需要做的是通过属性(获取器)访问字典。

mystruct s is initialized and won't give you a null reference exception. mystruct s已初始化,不会为您提供null引用异常。 As it initializes, it sets its members to their default values. 初始化时,将其成员设置为其默认值。 It therefore sets the a and b members to null since they are reference types. 因此,将ab成员设置为null因为它们是引用类型。

This might be the problem: 这可能是问题所在:

"When you create a struct object using the new operator, it gets created and the appropriate constructor is called. Unlike classes, structs can be instantiated without using the new operator. If you do not use new, the fields will remain unassigned and the object cannot be used until all of the fields are initialized." “当使用new运算符创建结构对象时,将创建它并调用适当的构造函数。与类不同,可以在不使用new运算符的情况下实例化结构。如果不使用new,则字段将保持未分配状态,并且该对象在所有字段都初始化之前不能使用。”

Perhaps either you haven't newed up your struct or some of those fields hidden behind the ... are not initialised yet? 也许您尚未更新结构,或者隐藏在...之后的某些字段尚未初始化?

http://msdn.microsoft.com/en-us/library/ah19swz4(VS.71).aspx http://msdn.microsoft.com/zh-CN/library/ah19swz4(VS.71).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM