Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9207649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:26:03+00:00 2026-06-18T00:26:03+00:00

Possible Duplicate: Array of an unknown length in C# I want to create a

  • 0

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();
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T00:26:04+00:00Added an answer on June 18, 2026 at 12:26 am

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Why is Array.Length an int, and not an uint Is there is
Possible Duplicate: Create an Array of the Last 30 Days Using PHP I am
Possible Duplicate: Most efficient way to create a zero filled JavaScript array? I wish
Possible Duplicate: Array versus List<T>: When to use which? Is there any best practice
Possible Duplicate: Javascript array sort and unique I was surprised to see that there
Possible Duplicate: Dynamic array in Stack? How do compilers treat variable length arrays Someone
Possible Duplicate: Javascript Array.sort implementation? I want to know how .sort() works in JavaScript.
Possible Duplicate: How can I create cartesian product of vector of vectors? I'm having
Possible Duplicate: Array length undefined I have the following array but whenever I try
Possible Duplicate: Char array gives error when initializing I want to insert an environment

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.