8

Possible Duplicate:
Array of an unknown length in C#

I want to create a program where users can input there items and those items will be stored in an array. When the user is okay with the amount of items, the program will ask for every item if he got it.

The problem is i cant seem to create an array that is of unknown size. I tried to use something like this: String[] list = new string[]{}; But when the program goes there it gives an IndexOutOfRangeException.

Is there a way i can do this?

This is the full code:

bool groceryListCheck = true;
        String[] list = new string[]{};
        String item = null;
        String yon = null;
        int itemscount = 0;
        int count = 0;

        while (groceryListCheck)
        {
            Console.WriteLine("What item do you wanna go shop for?");
            item = Console.ReadLine();
            list[count] = item;
            count++;
            Console.WriteLine("Done?");
            yon = Console.ReadLine();
            if (yon == "y")
            {
                groceryListCheck = false;
                itemscount = list.Count();
            }
            else
            {
                groceryListCheck = true;
            }
        }

        for (int x = 0; x < itemscount; x++)
        {
            Console.WriteLine("Did you got the " + list[x] + "?");
            Console.ReadKey();
        }
Community
  • 1
  • 1
Dallox
  • 487
  • 5
  • 8
  • 19
  • Arrays size must be known at initialization. They can not grow. What you want is a `List` – Rotem Jan 30 '13 at 16:26
  • 1
    There are a ton of C# array questions on StackOverflow - have you looked at them yet? Specifically http://stackoverflow.com/questions/599369/array-of-an-unknown-length-in-c-sharp ? – Ryan Cavanaugh Jan 30 '13 at 16:27

5 Answers5

15

Use a List instead of an array.

List<string> myList = new List<string>();
myList.Add("my list item");

After you have gathered all of the items, you can then use a foreach loop to iterate through all of the items in the collection.

foreach(string listItem in myList)
{
    Console.WriteLine(listItem);
}
Dave Zych
  • 21,581
  • 7
  • 51
  • 66
5

A List<string> would be much easier and more flexible.

There are lots of examples of using a List here which show you a variety of methods to extract data from them.

Bali C
  • 30,582
  • 35
  • 123
  • 152
4

You can use a List<string> and then, if you need an array as result, you can call the .ToArray() method.

888
  • 3,246
  • 8
  • 41
  • 60
1

I found that making variable list into a List worked. For instance:

        bool groceryListCheck = true;
        List<string> list = new List<string>();
        String item = null;
        String yon = null;
        int itemscount = 0;

        while (groceryListCheck)
        {
            Console.WriteLine("What item do you wanna go shop for?");
            item = Console.ReadLine();
            list.Add(item);
            Console.WriteLine("Done?");
            yon = Console.ReadLine();
            if (yon == "y")
            {
                groceryListCheck = false;
                itemscount = list.Count();
            }
            else
            {
                groceryListCheck = true;
            }
        }

        for (int x = 0; x < itemscount; x++)
        {
            Console.WriteLine("Did you got the " + list[x] + "?");
            Console.ReadKey();
        }

That is the full code and it works for me.

SuperPrograman
  • 1,814
  • 17
  • 24
0

I would say for this purpose you should use a Hashtable. You can add to it as much as you want and you do not need to specify the size when you create it. Kind of like a really simple database.

See: http://www.dotnetperls.com/hashtable

Mike Baxter
  • 6,868
  • 17
  • 67
  • 115