title: Why does my simple C# program quit immediately?
body: <p>I'm just starting out learning C#, and I'm working on a problem where I need to read a list of names (input by the user) and print them out again. I should accept up to 20 names. If the user enters null or QUIT, I should stop taking names. </p>
  
  <p>Here's what I've got so far:</p>
  
  <pre><code>namespace ConsoleApp1
  {
      class Program
      {
          static void Main(string[] args)
          {
              string[] names = new string[20]; // create a 20 name array
              int nameCount = 0;
              string userInput;
              Console.WriteLine("Enter a bunch of names!"); // ask for a name
              userInput = Console.ReadLine(); // store the name in userInput
              for (int maxNames = 20; maxNames &lt; names.Length; maxNames++)
              {
                  if (userInput == "") // if the entry is null, stop taking names.
                  {
                      Console.WriteLine("You entered {0}", names);
                      Console.ReadKey();
                  }
                  else if (userInput == "QUIT") // if the entry is QUIT, stop taking names.
                  {
                      Console.WriteLine("You entered {0}", names);
                      Console.ReadKey();
                  }
                  else // if it isn't null or QUIT, continue populating the array until we hit the max.
                  {
                      names[nameCount] = userInput;
                      nameCount = nameCount + 1;
                      maxNames = maxNames + 1;
                  }
  
  
              }
          }
      }
  }
  </code></pre>
  
  <p>When I run this, I get the "Enter a bunch of names!" prompt, but as soon as I enter a name, the console closes. I'm not sure what I'm doing wrong here. Any help for a newbie is appreciated.</p>
  
  <p>Update: Thanks everybody for all the help. I took a few different pieces of advice (and new shortcuts!) and ended up with this:</p>
  
  <pre><code>namespace ConsoleApp1
  {
      class Program
      {
          static void Main(string[] args)
          {
              string[] names = new string[20]; // create a 20 name array
              int nameCount = 0;
              int maxNames = 0;
              Console.WriteLine("Enter a bunch of names!"); // ask for a name
              while (maxNames != 20)
              {
                  string userInput = Console.ReadLine(); // store the name in userInput
                  if (userInput == "") // if the entry is null, stop taking names.
                  {
                      Console.WriteLine("You entered:");
                      foreach (string name in names)
                      {
                          Console.WriteLine(name);
                      }
                      Console.ReadKey();
                  }
                  else if (userInput == "QUIT") // if the entry is QUIT, stop taking names.
                  {
                      Console.WriteLine("You entered:");
                      foreach (string name in names)
                      {
                          Console.WriteLine(name);
                      }
                      Console.ReadKey();
                  }
                  names[nameCount] = userInput;
                  nameCount++;
                  maxNames++;
              }
              Console.ReadKey();
          }
      }
  }
  </code></pre>