Click here to Skip to main content
Sign Up to vote bad
good
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:46
Edited 19 Nov '12 - 2:52

Comments
Earloc - 19 Nov '12 - 8:59
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:06
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:29
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 - 19 Nov '12 - 16:52
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:06
thanks for post.

2 solutions

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  
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  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 8,286
1 OriginalGriff 6,561
2 CPallini 3,533
3 Rohan Leuva 2,703
4 Maciej Los 2,234


Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 20 Nov 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid