List of array of certain size?

Hello

I was wondering if it was possible to create a List of an array of a specific size? I tried to do it but i can’t work out the right syntax… My attempt was like this:

public List<MyClass[]> test = new List<MyClass[2]>();

But i get an error saying:

 error CS1519: Unexpected symbol `<'

Is it possible to do this with a List or will i have to find another way?

You are confusing 2 different, albeit similar, concepts. Lists change in size (re: count) dynamically, and so do not and cannot be set (from the searching I’ve done)
Arrays sizes (re: length) MUST be set.
Why? Because Lists are expected to contain something, whereas arrays are expected to be later filled with something. So while you could set up a for loop and just add a bunch of empty items to the list, I don’t know why in the world you would. Just add things to the list as you come to them.

1 Like

I know the difference, i wanted to combine them.

I wanted a list of arrays of size 2 so i had a list which had a pair for each one such as:

List[0]¬
===[0] data
===[1] data
List[1]¬  
===[0] data
===[1] data

The list allows me to remove and add these pairs easily but i know the pairs are a max size of 2 so an array suited better for the data.

Well, you can make lists of arrays. But an array in a list is just a reference to an actual array somewhere else in memory. The list doesn’t hold the actual array. So it can’t say anything about the size of the array. It just says that each element points to an array of a specific type.

You would be better off just making a struct with the two types of data you want in it, and then make a list of that struct type.

Oh i see so i create the array of the size i want separately and just add it, the List doesn’t care what size the array will be ?

Yes, you can create the arrays separately and add them. List doesn’t care about the size, all it cares about is that each one holds the same type of data.

Yes got it working now ! Thank you :slight_smile:

1 Like