using System;
class MainClass {
public static void Main (string[] args) {
Console.WriteLine("Press any key to continue...");
Console.WriteLine(" key pressed", Console.ReadKey());
}
}
this code works and has no errors but
using System;
class MainClass {
public static void Main (string[] args) {
Console.WriteLine("Press any key to continue...");
Console.WriteLine(Console.ReadKey(), " key pressed");
}
}
This doesn't work and I get an error
Error CS1502: the best overload method match for 'System.Console.WriteLine(string, object)' has some invalid arguments
and
Error CS1503: Argument '#1' cannot convert 'System.ConsoleKeyInfo' expression to type 'string'
I'm new to C# so I don't know much about the language (only ever used Python before), but in Python, I would write this code as
keyPressed = input("Type a key(s) ")
print(keyPressed, "is the key(s) you pressed")
I also can't just assign ReadKey() to a variable
var keyPressed = Console.ReadKey();
Console.WriteLine("the key you pressed was {0}", keyPressed);
For the block of code above, I want to have whatever key the user presses stored inside the variable keyPressed, but it doesn't work.
My question is why can't you put Console.ReadKey()
in front of the text I want displayed on the console, or assign Console.ReadKey()
to a variable, and how would you have whatever key the user presses assigned to a variable?