1

Im running .NET Core 2.2 and just want to create a simple Console Application that reads one value and prints it out. Yes i know there is millions of examples of this but my problem is that none of them works for some reason. When i press Enter the application stops.

Here is the simple code that just writes the first line but then do nothing:

    static void Main(string[] args)
    {
        Console.WriteLine("I want to write a value here");

        var theValue = Console.ReadLine();

        Console.WriteLine(theValue);
    }

Is there any configuration i have to do in Visual Studio 2017 to make the Console read my value i write? Or anything else that makes the Console close and program to end when i press enter?

jagge123
  • 263
  • 1
  • 5
  • 15
  • `When i press Enter the application stops.` do you mean closing? Then just put `Console.ReadLine();` after all – demo May 21 '19 at 15:03
  • What do you mean by "stops" and "do nothing"? If you enter text and hit "Enter" your application should be displaying that for a tiny fraction of a second and then closing. You don't have any code to hold the console open. – Broots Waymb May 21 '19 at 15:04

2 Answers2

4

The application doesn't stop, It simply ends since it prints out the result of your line and moves to next line since there is no more code to run it simply closes the application

Add Console.ReadLine(); on the end so that it doesn't close after first enter

    static void Main(string[] args)
    {
        Console.WriteLine("I want to write a value here");

        var theValue = Console.ReadLine();

        Console.WriteLine(theValue);

        Console.ReadLine();
    }
Tiago Silva
  • 2,299
  • 14
  • 18
  • If you don't want to add the extra ReadLine and you don't need to step through while debugging you can start the app with Ctrl-F5 ("Start without Debugging" on the Debug menu.) When you run it this way Visual Studio leaves the console window open for you. – Mike May 21 '19 at 16:28
0

you need to make the app wait and not exit using an additional Console.Read. Try like:

var theValue = Console.ReadLine();
Console.WriteLine(theValue);
Console.Read();
apomene
  • 14,282
  • 9
  • 46
  • 72