I have a char
array and I want to assign values from the console. Here\'s my code:
char[] input = new char[n];
for (int i = 0; i < input.Leng
Use Console.ReadKey and then KeyChar
to get char
, because ConsoleKeyInfo is not assignable to char
as your error says.
input[i] = Console.ReadKey().KeyChar;
Quick example to play around with:
public static void DoThis(int n)
{
var input = new char[n];
for (var i = 0; i < input.Length; i++)
{
input[i] = Console.ReadKey().KeyChar;
}
Console.WriteLine(); // Linebreak
Console.WriteLine(input);
Console.ReadKey();
}
Grab the first character of the String being returned by Console.ReadLine()
char[] input = new char[n];
for (int i = 0; i < input.Length; i++)
{
input[i] = Console.ReadLine()[0];
}
This will throw away all user input other than the first character.