Reputation: 13
I am new to programming and as seems to be traditional I tried to create a "hello world" program in C#; however, as soon as I run the program it closes.
This is my code inside:
main()
console.writeline("hello world");
console.writeline("enter name");
console.writeline("where is the frikin console");
It's really annoying and I know it might be something simple for the additional users but how do I keep the window open.
Upvotes: 1
Views: 1393
Reputation: 537
The window automatically closes after your program, you need to let it take some input, for example:
Add
Console.ReadLine();
Which takes a line of input (till "\n"). And your program will wait until somebody hit the return key (they can type anything and the program won't close: until you hit the return key. You can type in "hello world back what's up are you ok?" and nothing will happen.)
or
Console.ReadKey();
Which will take a character of input. This will make your program wait for the user to press any key and then closes.
Upvotes: 0
Reputation: 59
It's really quite simple.
After this line of code:
Console.WriteLine("where is the frikin console");
You need to add this:
Console.ReadLine();
That should work.
The reason the console closes is because you told it to write some stuff to the screen, after it has finished writing what you told it to write it simply closes itself all in the fraction of a second. if you add Console.ReadLine
, the console will wait for you to input something before closing, like pressing a key on the keyboard.
Upvotes: 1
Reputation: 1175
Use
Console.ReadLine();
in the end of your code. You are having this problem because the program just write the message then it ends, that's why you can't see anything. By adding that line, you keep the program waiting something to be typed and you can read the message. After this, type something to end the program.
Upvotes: 0
Reputation: 8511
You can build your program and run the exe from the command line, that will allow you to see the output.
If you want the program to remain running then adding the Read() statement is the traditional approach, as others have already said.
If you just want to see it in debugging and do not want or need the read statement then place a breakpoint at the end of the program during a debug session.
Upvotes: 2
Reputation: 1760
You could use CTRL
+ F5
which will opens the command line and after execution of your code, it shows Press any key to continue...
. This will be handy for you than adding few lines of code additionally.
Upvotes: 0
Reputation: 9729
Use Console.ReadLine();
or Console.ReadKey();
at the end of your program to wait for the return key or for any key.
Upvotes: 2
Reputation: 5895
Console.WriteLine("hello world");
Console.WriteLine("enter name");
Console.WriteLine("where is the frikin console");
Console.ReadLine();
Console.ReadLine();
will close the console after you've hit (for example) enter.
Console.ReadKey();
will close the console after the next key-hit
You can read the console-contents with these methods,too
Console.WriteLine("hello world");
Console.WriteLine("enter name");
string name = Console.ReadLine();
Console.WriteLine("Your name is: " + name);
Console.ReadLine();
Upvotes: 1