Complete the code to box the integer value into an object.
int num = 10; object boxedNum = [1];
Boxing is done by converting the value type to object explicitly or implicitly. Here, casting num to object boxes it.
Complete the code to unbox the object back to an integer.
object boxedNum = 20; int unboxedNum = [1];
Unboxing requires an explicit cast from object to the original value type. Using (int)boxedNum correctly unboxes the value.
Fix the error in the unboxing code to avoid InvalidCastException.
object boxedValue = 15; long unboxedValue = [1];
The object holds an int, so unboxing must first cast to int, then convert to long. Direct cast to long causes an error.
Fill both blanks to create a dictionary with boxed integers and unbox them safely.
var dict = new Dictionary<string, object> {
{"one", [1],
{"two", [2]
};The dictionary stores values as objects, so integers 1 and 2 are boxed automatically when added.
Fill all three blanks to unbox values from the dictionary and sum them.
var dict = new Dictionary<string, object> {
{"a", 5},
{"b", 10}
};
int sum = (int)dict[[1]] + (int)dict[[2]] + [3];Access dictionary values by their string keys and unbox them to int. Adding 0 completes the sum correctly.