4

I would like to get the user enters two different values using only ONE statement of Console.ReadLine(). More specifically, instead of using two different variables declarations with two different ReadLine() method - I want to let the user enters two variables at the same time using one ReadLine() method for further processing.

Alex Aza
  • 76,499
  • 26
  • 155
  • 134
Sinan
  • 453
  • 2
  • 10
  • 20

2 Answers2

7

Something like this?

var line = Console.ReadLine();
var arguments = line.Split(' ');
var arg1 = arguments[0];
var arg2 = arguments[1];

In this example, space delimits the arguments. You can use , or ; as a delimiter, if you would like to.

QMaster
  • 3,743
  • 3
  • 43
  • 56
Alex Aza
  • 76,499
  • 26
  • 155
  • 134
2
Console.WriteLine("Enter values separated by space");
string input = Console.ReadLine();
string[] inputs = input.Split(' ');
foreach (string inp in inputs)
{
     Console.WriteLine(inp);  
}
Bala R
  • 107,317
  • 23
  • 199
  • 210