0
votes

I have data that I would ideally want to represent as so:

LinkedList<T>[]

However, you cannot do that on generics, so I wrapped it in a struct:

    public struct SuitList                      
    {
        LinkedList<T> aList;

        public SuitList()
        {
            aList = new LinkedList<T>();
        }
    }

Now, in my class I have

   SuitList[] myStructList;      //there is only 4 indices of myStructList

How do I initialize aList, inside my constructor for the class? I tried this as follows:

myStructList = new SuitList[4];

for(int i = 0; i < 4; i++)
{
    myStructList[i] = new SuitList();
}

The compiler gave me an error saying Structs cannot contain explicit parameterless constructors. Are there better ways of doing this? Thanks for the help in advance.

2
That sounds like a very strange construct. Why would you want an array of linked lists?SLaks
What error did you receive when you tried to use a LinkedList<T>[]? There's nothing wrong with it.phoog
@phoog, you are correct. I was mistakenCodeKingPlusPlus
@Slaks, My array is holding 4 linked-lists where each linked-list has a common indexed attribute. Thus, I have convenient access to each list with a specified attribute.CodeKingPlusPlus

2 Answers

7
votes

C# is not Java.
You can do that just fine with generics.

To answer your question, you should create a class, not a struct.

0
votes

You can do this without it having to be a class.

public struct SuitList<T>
    {
        LinkedList<T> aList;

        public SuitList(int nothing = 0)
        {
            aList = new LinkedList<T>();
        }
    }