Click here to Skip to main content
12,501,207 members (41,602 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
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
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
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
   
thanks for post.
Rate this: bad
 
good
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.
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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

//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>()
}
 
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
  Permalink  
v3

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.160919.1 | Last Updated 20 Nov 2012
Copyright © CodeProject, 1999-2016
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