8

I am switching from Python to C# and I am having trouble with the ReadLine() function. If I want to ask a user for input Python I did it like this:

x = int(input("Type any number:  ")) 

In C# this becomes:

int x = Int32.Parse (Console.ReadLine()); 

But if I type this I get an error:

int x = Int32.Parse (Console.ReadLine("Type any number:  "));

How do I ask the user to type something in C#?

BJ Myers
  • 6,617
  • 6
  • 34
  • 50

5 Answers5

7

You should change this:

int x = Int32.Parse (Console.ReadLine("Type any number:  "));

to this:

Console.WriteLine("Type any number:  "); // or Console.Write("Type any number:  "); to enter number in the same line

int x = Int32.Parse(Console.ReadLine());

But if you enter some letter(or another symbol that cannot be parsed to int) you will get an Exception. To check if entered value is correct:

(Better option):

Console.WriteLine("Type any number:  ");

int x;

if (int.TryParse(Console.ReadLine(), out x))
{
    //correct input
}
else
{
    //wrong input
}

Starting from C# 7 you can use inline variable declaration (out variables):

Console.WriteLine("Type any number:  ");

if (int.TryParse(Console.ReadLine(), out var x)) // or out int x
{
    //correct input
}
else
{
    //wrong input
}
Roman
  • 11,966
  • 10
  • 38
  • 47
  • One imp thing... If you are not sure about the input and want to check it first (which is a good idea) try TryParse instead of Parse... – A3006 Feb 21 '17 at 12:04
  • 1
    Note that you can now declare the variable inline: `int.TryParse(Console.ReadLine(), out int x)`. It will be accessible in the `if`. – Flater Mar 17 '22 at 14:38
1
Console.WriteLine("Type any number");
string input = Console.ReadLine();
int x;
if (int.TryParse(input, out x))
{
    //do your stuff here
}
else
{
    Console.WriteLine("You didn't enter number");
}
Nino
  • 6,931
  • 2
  • 27
  • 42
0
Console.WriteLine("Type any number: ");
string str = Console.ReadLine();
Type a = Type.Parse(str);

where Type is Data Type you want to cast user input to. I suggest reading few books on C# fundaments before turning to forums.

Vivek
  • 580
  • 2
  • 7
  • 26
0

To be more generic I would suggest you to make an additional object ( because you cannot extend static objects in C# ) to behave like you've specified.

public static class ConsoleEx
{
    public static T ReadLine<T>(string message)
    {
        Console.WriteLine(message);
        string input = Console.ReadLine();
        return (T)Convert.ChangeType(input, typeof(T));
    }
}

Of course you this code is not error free because it does not contains any constraints about the output type but still It will cast into some types without any problems.

For example. Using this code :

static void Main()
{
    int result = ConsoleEx.ReadLine<int>("Type any number: ");
    Console.WriteLine(result);
}

>>> Type any number: 
<<< 1337
>>> 1337 

Check this online

mrogal.ski
  • 5,828
  • 1
  • 21
  • 30
-1

try this

Console.WriteLine("Type any number:  ");
int x = Int32.Parse (Console.ReadLine());
imsome1
  • 1,182
  • 4
  • 22
  • 37