Junior — Senior
Boxing and unboxing of values in C#
livecode
Task condition
In the function Example2, the following occurs:
- An object
objis created, which encapsulates the integer value 5. - Then, the value is extracted (unboxed) from
objand stored in the variablenumof typeint. - The variable
numis assigned a new value of 12. - After that, the contents of
objand the current value ofnumare printed to the console.
Question: what exactly will be printed and what is their explanation?
static void Example2()
{
object obj = 5;
int num = (int)obj;
num = 12;
Console.WriteLine(obj);
Console.WriteLine(num);
}