I'm trying to understand what is happening with Console.ReadKey()
versus Console.ReadKey(true)
. I understand that including true
will prevent the key from being displayed to the console, but there is some odd behavior produced when also using Console.Clear();
I should say that this C# code is being compiled and run on a Pixel C (android) tablet. I don't know if it behaves the same on Windows.
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
namespace CSharp_Shell
{
public static class Program
{
public static void Main()
{
string[] str = new String[5] {"one","two","three","four","five"};
foreach(string s in str)
{
Console.WriteLine(s);
}
Console.ReadKey(true);
Console.Clear();
foreach(string s in str)
{
Console.WriteLine(s);
}
}
}
}
When executed, this console will display...
one
two
three
four
five
...after which I can press a key, clearing the console, and displaying the text again.
However, if I change Console.ReadKey(true);
to Console.ReadKey();
then the execution produces the initial text as expected, and after pressing a key only displays...
one
two
Finally, running the code in the two ways described above but with the additional change of removing the Console.Clear();
line results in all the text being displayed as expected (without the screen clearing), with the difference being that when using Console.ReadKey();
any displayable key pressed is also written to the console (which I would expect).