Declaring an array of List Generic Class elements

G

Guest

Hello,

I am declaring an element like this:
public static List<myListElement> myList = new List<myListElement>();

I would like to declare an array of this myList.
How would I modify the declaration of myList so it will represents an array?

Thanks
Eitan
 
C

colin

Eitan said:
Hello,

I am declaring an element like this:
public static List<myListElement> myList = new List<myListElement>();

I would like to declare an array of this myList.
How would I modify the declaration of myList so it will represents an
array?

Thanks
Eitan

public static List<myListElement>[] myList = new List<myListElement>[4];

Colin =^.^=
 
G

Guest

Hello Colin,
Thank you for your answer.

I tried the line below (before posting my question):
public static List<myListElement>[] myList = new
List<myListElement>[4]();
The difference is the brackets "()" after "[4]". The compiler generated an
error.

Can you explain to me why the error, and when would you need the brackets
"()"?

Thank you,
Eitan


colin said:
Eitan said:
Hello,

I am declaring an element like this:
public static List<myListElement> myList = new List<myListElement>();

I would like to declare an array of this myList.
How would I modify the declaration of myList so it will represents an
array?

Thanks
Eitan

public static List<myListElement>[] myList = new List<myListElement>[4];

Colin =^.^=
 
T

Tom Spink

Eitan said:
Hello,

I am declaring an element like this:
public static List<myListElement> myList = new List<myListElement>();

I would like to declare an array of this myList.
How would I modify the declaration of myList so it will represents an
array?

Thanks
Eitan

Hi Eitan,

You can declare it like this:
public static List<myListElement>[] listArray = new List<myListElement>[10];

(To define an array of size 10)

But please note that each element in the array will still have to be
constructed. You could this in a for-loop:

///
for (int i = 0; i < listArray.Count; i++)
listArray = new List<myListElement>();
///
 
T

Tom Spink

Eitan said:
Hello Colin,
Thank you for your answer.

I tried the line below (before posting my question):
public static List<myListElement>[] myList = new
List<myListElement>[4]();
The difference is the brackets "()" after "[4]". The compiler generated
an error.

Can you explain to me why the error, and when would you need the brackets
"()"?

Thank you,
Eitan


colin said:
Eitan said:
Hello,

I am declaring an element like this:
public static List<myListElement> myList = new List<myListElement>();

I would like to declare an array of this myList.
How would I modify the declaration of myList so it will represents an
array?

Thanks
Eitan

public static List<myListElement>[] myList = new List<myListElement>[4];

Colin =^.^=

Hi Eitan,

If you take a look at my response it may be a bit clearer what is going on.
When you're defining the array, you aren't constructing the List<> objects,
just constructing an array of list objects. Parentheses are not required
for constructing an array, as the construction doesn't take parameters,
only a size of array to create given in the square brackets.

So, that being the case, you'll have to manually construct each List<>
object in your array.
 
G

Guest

Thank you!

Tom Spink said:
Eitan said:
Hello,

I am declaring an element like this:
public static List<myListElement> myList = new List<myListElement>();

I would like to declare an array of this myList.
How would I modify the declaration of myList so it will represents an
array?

Thanks
Eitan

Hi Eitan,

You can declare it like this:
public static List<myListElement>[] listArray = new List<myListElement>[10];

(To define an array of size 10)

But please note that each element in the array will still have to be
constructed. You could this in a for-loop:

///
for (int i = 0; i < listArray.Count; i++)
listArray = new List<myListElement>();
///
 
G

Guest

Tom,

Thank you on both of your answers!

Eitan

Tom Spink said:
Eitan said:
Hello Colin,
Thank you for your answer.

I tried the line below (before posting my question):
public static List<myListElement>[] myList = new
List<myListElement>[4]();
The difference is the brackets "()" after "[4]". The compiler generated
an error.

Can you explain to me why the error, and when would you need the brackets
"()"?

Thank you,
Eitan


colin said:
Hello,

I am declaring an element like this:
public static List<myListElement> myList = new List<myListElement>();

I would like to declare an array of this myList.
How would I modify the declaration of myList so it will represents an
array?

Thanks
Eitan


public static List<myListElement>[] myList = new List<myListElement>[4];

Colin =^.^=

Hi Eitan,

If you take a look at my response it may be a bit clearer what is going on.
When you're defining the array, you aren't constructing the List<> objects,
just constructing an array of list objects. Parentheses are not required
for constructing an array, as the construction doesn't take parameters,
only a size of array to create given in the square brackets.

So, that being the case, you'll have to manually construct each List<>
object in your array.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top