Click here to Skip to main content
13,049,343 members (63,344 online)
Rate this:
Please Sign up or sign in to vote.
See more:
How can we do like this.

general arrays like this below.
class []emp  = new emp[3];

but how to do using Generics
List<emp>[]emp = new List<emp>[3];

using collection initialisation I know:
like List<class>obj = new List<obj>{
new class{},
new class{},
new class{}

guide me,is this possible or not.
Posted 19-Nov-12 2:46am
Updated 19-Nov-12 2:52am
Earloc 19-Nov-12 8:59am
please tell us more about your intensions to do this!
1.) do you want an array of instances of a generic type?
2.) or a generic List which elements are Arrays of a type?
Michiel du Toit 19-Nov-12 10:29am
var lst = new List<myclass>() will create a generic list that contains elements of type myclass.

Since lists do not have a fixed size there is no syntax such as new List<myclass>[10] - you can add items using lst.Add(instanceof_myclass) or lst.AddRange(arrayof_myclass).
Mathlab 19-Nov-12 16:52pm
Its definitely possible, I've created several custom types and stored them using the Lists.

Give me some more info on what you want to do and I'll post an example.
Member 9600648 20-Nov-12 0:06am
Array of instances of a generic type,
i remember , like after creatig the generic list then list.ToArray();
right Earloc, that is way ?
thanks your reply.
Member 9600648 20-Nov-12 0:06am
thanks for post.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You don't give a size, you just do new List<emp>(), because it's an empty collection you add items to, it can change size. An array cannot, so you have to tell it how big it is.

You can create an array with { 1,2,3 }, so I imagine if the Array class has a ToList ( and I think it does ), then you could use it in the manner you imagine, although it's as easy to push the three objects in after creating it.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

1.) Generic List of Integers:
List<int> intList = new List<int>();

//int[] fixedSizeArray = intList.ToArray();

2.) List of Integer-Arrays:

List<int[]> intArrayList = new List<int[]>();
intArrayList.Add(new int[]{1, 2, 3});
intArrayList.Add(new int[]{4, 5, 6});
intArrayList.Add(new int[]{7, 8, 9});

//int[][] intArrayArray = intArrayList.ToArray();

3.) Array of List of Integers:

List<int>[] listArray = new List<int>() {
  new List<int>(),
  new List<int>(),
  new List<int>()

For clarity, i did not use the var-keyword.
Thats how you would build up the various constructs, case 3.) is possible, but doesnt feel right. However, it all depends on what you are trying to achieve.
Remember to initialize instances of List<t> with a count, if you know how many calls to Add you are going to do (that is, how much elements the List-instance will hold), so the Runtime can reserve enough space for the elements. Otherwise the runtime will enlarge the List every 4 (or so)Elements, which will result in poor performance (refer MSDN [^]for mor details)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 20 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100