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
VulpesPosted Jul 6, 2012, 11:19 AM
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.
Guest UserPosted Jul 6, 2012, 11:20 AM
Guest UserPosted Jul 6, 2012, 11:06 AM
"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?
VulpesPosted Jul 6, 2012, 10:51 AM
http://msdn.microsoft.com/en-us/library/system.consolekey.aspx