Click here to Skip to main content
Rate this: bad
good
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 3:46am
Edited 19-Nov-12 3:52am
v3
Comments
Earloc at 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 at 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 at 19-Nov-12 10:29am
   
var lst = new List() 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[10] - you can add items using lst.Add(instanceof_myclass) or lst.AddRange(arrayof_myclass).
Mathlab at 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 at 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 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)
  Permalink  
v3

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

  Print Answers RSS
0 OriginalGriff 7,215
1 DamithSL 5,114
2 Maciej Los 4,866
3 Sergey Alexandrovich Kryukov 4,747
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web01 | 2.8.141223.1 | Last Updated 20 Nov 2012
Copyright © CodeProject, 1999-2014
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