0

I want to add the below data into array in c# , but I dont know the size of the array. I have searched many questions, but all array's size in predefine.

I dont want to define the the size of the array as data will come from DB run-time.

so how can I add the below data into Array in C# ?

 (Id : Value) 


    1 : 500 ,
    2 : 700 , 
    3 : 800 ,
    4 : 900 ,
    .
    .
    .
    Upto no of records into DB. 

I have the below code and want to add data to array like below code.

   foreach (var LocationObj in Location)
        {
           StockList.Add(InventoryDTO.Id, InventoryDTO.Quantity);
        }

Cant use dictionary, as key might be duplicate.

Want simple code of 2D List...

bnil
  • 1,531
  • 6
  • 35
  • 68
  • 6
    Why not use a List? – Tim May 13 '14 at 09:54
  • 2
    Is there a specific reason why you want to use an array instead of something that's easier to handle dynamically? – germi May 13 '14 at 09:54
  • And eventually if you want to return an array, you can just convert the List back to an Array – Yahya May 13 '14 at 09:56
  • If I can convert List into Array (2-D) then its fine... may get any link to add the data in 2D list ? – bnil May 13 '14 at 09:57
  • You should also consider using [Dictionary](http://msdn.microsoft.com/en-us/library/xfhwa508(v=vs.110).aspx?cs-save-lang=1&cs-lang=csharp) – bansi May 13 '14 at 09:58
  • @user1650894 but why would you need to use array specifically? – Tarec May 13 '14 at 09:58
  • I have to pass it to Javascript... I am not sure list will work their... – bnil May 13 '14 at 09:59
  • @user1650894: if you need an array you can always call List.toArray() – RvdK May 13 '14 at 10:02
  • List is the best option, however if still you need array then you can use yourList.ToArray() to get list type array. Every single item in array will be object of your list type. – Lali May 13 '14 at 10:03
  • Can you provide the simple example for adding 2D list ? – bnil May 13 '14 at 10:10
  • @user1650894 `List> my2DList = new List>();` and then fulfill it like this `my2DList.Add(new List())` or another way: `var internalList = new List()` and `my2DList.Add(internalList);` – Tarec May 13 '14 at 10:14

4 Answers4

2

Well, if in all answers people tell you array's size has to be predefined, it means the array's size has to be predefined.

Use dynamic size collections, like List.

Tarec
  • 3,268
  • 4
  • 30
  • 47
2

You can use a List and convert it to an array.

List<Location> StockList = new List<Location>();
// add some data
 foreach (var LocationObj in Location)
 {
     StockList.Add(InventoryDTO);
 }
// convert to array
var myArray = StockList.ToArray();

If your Location is unique you should use a dictionary.

 Dictionary<int, Location> StockList= new Dictionary<int, Location>();
 foreach (var LocationObj in Location)
 {
     StockList.Add(InventoryDTO.Id, InventoryDTO.Quantity);
 }

More Information

dknaack
  • 60,192
  • 27
  • 155
  • 202
0

Use a List instead of array.

List<Location> Locations = new List<Location>();
// There is no need to specify a size
// Now you can add how many Location instances you need

Edit: If you need to store multiple values, use a List of Tuple.

// We assume Id and Quantity are integers
List<Tuple<int, int>> StockList = new List<Tuple<int, int>();
foreach (var LocationObj in Location)
    {
       StockList.Add(new Tuple<int, int>(InventoryDTO.Id, InventoryDTO.Quantity));
    }
Gabriel Negut
  • 13,860
  • 4
  • 38
  • 45
0

Try ArrayList

ArrayList myAL = new ArrayList();
myAL.add("itemname");

Checkout here

http://msdn.microsoft.com/en-us/library/system.collections.arraylist.aspx

Sam
  • 41
  • 2
  • 13