Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read char from the console

Tags:

c#

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.Length; i++)
{
    input[i] = Console.ReadLine();
}

But I'm getting the following error:

Cannot implicitly convert type 'System.ConsoleKeyInfo' to 'char'

Is there an easy way to fix this?

like image 570
Martin Dzhonov Avatar asked Nov 08 '13 13:11

Martin Dzhonov


1 Answers

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;
like image 174
Kamil Budziewski Avatar answered Sep 18 '22 18:09

Kamil Budziewski