Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How can we do like this.

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

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

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

guide me,is this possible or not.
Posted
Updated 19-Nov-12 2:52am
v3
Comments
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?
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.
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).
[no name] 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    
thanks for post.

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.
 
Share this answer
 
v2
1.) Generic List of Integers:
C#
List<int> intList = new List<int>();

intList.Add(1);
intList.Add(2);
intList.Add(3);


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


2.) List of Integer-Arrays:

C#
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:

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

listArray[0].Add(1);
listArray[0].Add(2);
listArray[0].Add(3);
listArray[1].Add(4);
listArray[1].Add(5);
listArray[1].Add(6);


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)
 
Share this answer
 
v3

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900