4
Answers

Retrieving data from a property

S

S

12y
1k
1
Hi


Given the following code I came across:-

ConsoleKeyInfo cki;

cki = Console.ReadKey();

Console.WriteLine("Key pressed: {0}\n", cki.Key);

if (cki.Key == ConsoleKey.X) 

break;


My question is, how is the Key property holding the required data without even being set?

The only thing I can see happening is that the instance variable cki is storing the return value captured from the ReadKey method. 

Id usually expect to see the something like:-

ConsoleKeyInfo cki = new ConsoleKeyInfo();

cki.Key = Console.ReadKey();

Console.WriteLine("Key pressed: {0}\n", cki.Key);

Regards

Steven
Answers (4)