Q:

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

A:

Since your declaration of dictionary a doesn't instantiate it, you are trying to add an item to a null. This is assuming you marked them public, otherwise that wouldn't compile.

A:

Some suggestions to improve your code:

Don't use a struct, use a class instead. Structs in .NET are a little different and unless one understands those differences I doubt one will ever have a valid use for structs. A class is almost always what you want.
ArrayList is more or less obsolete, it's almost always better to use a generic List<T> instead. Even if you need to place mixed objects in the list, List<object> is a better choice than ArrayList.
Make sure your members are properly initialized and not null before you access methods or properties of them.
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);
...

A:

The problem is that neither a nor b are initiated. 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);

