How to read char from the console

I have a char array and I want to assign values ​​from the console. Here is my code:

 char[] input = new char[n]; for (int i = 0; i < input.Length; i++) { input[i] = Console.ReadLine(); } 

But I get the following error:

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

Is there an easy way to fix this?

+7
c #
source share
2 answers

Use Console.ReadKey and then KeyChar to get char , because ConsoleKeyInfo not assigned char , as your error says.

 input[i] = Console.ReadKey().KeyChar; 
+24
source share

A quick example for the game:

  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(); } 
0
source share

All Articles