0

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.

4
  • 2
    That sounds like a very strange construct. Why would you want an array of linked lists? Commented Feb 5, 2012 at 23:38
  • 1
    What error did you receive when you tried to use a LinkedList<T>[]? There's nothing wrong with it. Commented Feb 5, 2012 at 23:44
  • @phoog, you are correct. I was mistaken Commented Feb 6, 2012 at 0:36
  • @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. Commented Feb 6, 2012 at 0:39

2 Answers 2

7

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

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

Sign up to request clarification or add additional context in comments.

Comments

0

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>();
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.