Complete the code to box the integer value into an object.
int number = 42; object boxedNumber = [1];
Boxing an integer means converting it to an object type. Using (object)number explicitly boxes the value.
Complete the code to unbox the object back to an integer.
object boxedNumber = 100; int originalNumber = ([1])boxedNumber;
Unboxing requires casting the object back to the original value type, which is int here.
Fix the error in the code that tries to unbox an object to a double.
object boxedValue = 10; double value = ([1])boxedValue;
The object actually holds an int, so unboxing must use int first, then convert to double if needed.
Fill both blanks to create a dictionary that maps integers to their boxed objects only if the integer is greater than 5.
var numbers = new List<int> {3, 7, 10};
var dict = numbers.Where(n => n [2] 5).ToDictionary(n => n, n => [1]);We box each integer with (object)n and filter with n > 5 to include only numbers greater than 5.
Fill all three blanks to create a method that accepts an object, checks if it is a boxed int, and returns its value multiplied by 2.
public int DoubleIfBoxedInt(object obj) {
if (obj is [1] value) {
return value [2] 2;
}
return [3];
}The is int value pattern unboxes the object if it holds an int. Then multiply by 2 with *. Return 0 if not an int.