0

I'm learning to program in C# and I'm having problems with the console, when I run the code below the console show the output but immediately close the window and I can't see anything. I don't know what to do to keep the window open. Any suggestion? I'll be very grateful.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace testApp_1
{
    class Program
    {
        static void Main(string[] args)
        {
            String word = "Hello world!";

            Console.WriteLine(word);
        }
    }
}
Sam Holder
  • 32,535
  • 13
  • 101
  • 181
German Rocha
  • 179
  • 1
  • 1
  • 5

6 Answers6

2

This happens because when you reach the last line, you also reach the end of the program, thus it terminates. To keep the console window open, just add Console.ReadKey() as the last line.

Tobias
  • 2,811
  • 2
  • 20
  • 31
2

The execution continues after writing to the console and then the program exits. you need to do something to pause the execution, usually you would read something from the console:

Console.ReadKey(); 

to pause until the user presses a key

so your whole program might look like:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace testApp_1
{
    class Program
    {
        static void Main(string[] args)
        {
            String word = "Hello world!";

            Console.WriteLine(word);

            //if you want the user to exit with any key press do this
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();

            //if you want the user to hit 'enter' to exit do this
            Console.WriteLine("Press enter to exit.");
            Console.ReadLine();
        }
    }
}

you can read about Console.ReadKey and Console.ReadLine on MSDN

Sam Holder
  • 32,535
  • 13
  • 101
  • 181
1

Your code closes immediately, because it doesn't have anything else to do. What you'll want to do is to add this to the bottom of your code:

Console.Readline();

That will cause it to wait until you press Enter.

krillgar
  • 12,596
  • 6
  • 50
  • 86
0

If you hit ctrl + F5 ( run without debugging) it will not close. But if you run with debugging, it will close after execution. You can use Console.ReadLine(); in the end to wait for the user to hit return.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace testApp_1
{
    class Program
    {
        static void Main(string[] args)
        {
            String word = "Hello world!";

            Console.WriteLine(word);
            Console.ReadLine();
        }
    }
}
Bilal Fazlani
  • 6,727
  • 9
  • 44
  • 90
0

You can use following method to read some user input: https://msdn.microsoft.com/de-de/library/system.console.readline%28v=vs.110%29.aspx

But you have to do some input.

You can also use logger frameworks: log4net: http://logging.apache.org/log4net/

Olli Zi
  • 325
  • 2
  • 10
0

Console.ReadLine() is missing.

Kenner Dev
  • 327
  • 1
  • 4
  • 13