user3477016
user3477016

Reputation: 145

Why does the ReadLine () method NOT pause for user input?

I began to study C#, but I ran into a problem. The ReadLine() method does not pause for user input. I just started C#, and it works on other people's programs, so I have no idea why. I am using Xamarin on a Mac.

Here is an example code that does not work:

using System;

namespace LearningC
{
    public class LearningSharp
    {
        public static void Main (String[] args)
        {
            Console.WriteLine (Console.ReadLine ());
        }
    }
}

Upvotes: 3

Views: 4550

Answers (3)

sKo
sKo

Reputation: 31

I have the same problem of a C# project not stopping with Console.ReadLine().

Here is my code:

char myChar1;
        string userName;
        string userLastname;

        Console.Write($"Insert a char: ");
        myChar1 = (char)Console.Read();

        Console.WriteLine($"Press ENTER to continue");
        Console.ReadLine();
        Console.Clear();

        Console.Write($"Insert name: ");
        userName = Console.ReadLine();

        Console.Write($"Insert lastname: ");
        userLastname = Console.ReadLine();
     
        // More stuff here

I found that when I use the method Console.Read() the next method Console.ReadLine() does not stop at all. So after inserting a char in the variable myChar1 the program goes forward and stops only after the method Console.Write($"Insert name: ");

The workaround is to place two Console.ReadLine() methods before the Console.Clear() one but I can not understand why this goes like that.

Upvotes: 1

user3477016
user3477016

Reputation: 145

I found out the solution. Right click on your project and select Options. Then, select General under the Run tab and check "Pause console output" ("Run on external console" must be checked for this to work.)

Upvotes: 7

Orkun Bekar
Orkun Bekar

Reputation: 1441

Maybe your Project is set to be a Windows application. Right click on your Project name, click properties and change output type to Console Application.

Upvotes: 5

Related Questions