Enter statement
In order for the user to be able to set the value of the variable himself, it is necessary to be able to enter values from the keyboard.
C# has two value input operators:
Console.Read();
and
Console.ReadLine();
Read
reads only one character from the entered values, or -1 if there are no more characters left to read. Moreover, the method returns an integer character code, so to get a character variable, you need to perform a conversion using the
Convert.ToChar()
.
int x = Console.Read(); // read character code
char a = Convert.ToChar(x); // converting the received code into the value of a character variable
With
ReadLine()
, you can read a string sequence before entering a new line. As a result, the method may return a string or
null
if there are no more strings.
For example, the entry reads the line:
stringline = Console.ReadLine();
To read an integer value, you need to read the string and convert it to a number:
int a = int.Parse(Console.ReadLine());
If the numbers go in a line, then you need to count the line, & nbsp; and get an array of strings from it using the space character as a separator. And then each element of the array is converted to a number:
string[] numbers = Console.ReadLine().Split(' ');
int a = int Parse(numbers[0]);
int b = int.Parse(numbers[1]);