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

VulpesRank #55 · 29.2K reputationPosted Jul 6, 2012, 7:19 AM EDT
cki = Console.ReadKey();
is creating a ConsoleKeyInfo structure and storing it in the memory slot reserved for the local variable cki on the stack. So cki actually contains the structure rather than a reference to where it's stored elsewhere in memory.
Whilst you're right that structures aren't objects, the terms 'instance' and 'instantiation' can be (and often are) used when creating either of them.
VulpesRank #55 · 29.2K reputationPosted Jul 6, 2012, 6:51 AM EDT
http://msdn.microsoft.com/en-us/library/system.consolekey.aspx
Guest UserRank #4,868 · 321 reputationPosted Jul 6, 2012, 7:06 AM EDT
"Is automatically set when the ConsoleKeyInfo structure is instantiated."
Is this referring to
ConsoleKeyInfo cki;
cki = Console.ReadKey();
I thought instantiatation was creating a "new" object?
Guest UserRank #4,868 · 321 reputationPosted Jul 6, 2012, 7:20 AM EDT