Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
196 views
Welcome To Ask or Share your Answers For Others

1 Answer

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...