0
0
C Sharp (C#)programming~10 mins

Performance implications of boxing in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to box the integer value into an object.

C Sharp (C#)
int number = 42;
object boxedNumber = [1];
Drag options to blanks, or click blank then click option'
A(object)number
Bnumber.ToString()
Cnumber
Dref number
Attempts:
3 left
💡 Hint
Common Mistakes
Using the variable directly without casting does not box it explicitly.
Using ToString() converts to string, not boxing.
Using ref keyword is for references, not boxing.
2fill in blank
medium

Complete the code to unbox the object back to an integer.

C Sharp (C#)
object boxedNumber = 100;
int originalNumber = ([1])boxedNumber;
Drag options to blanks, or click blank then click option'
Astring
Bint
Cobject
Ddouble
Attempts:
3 left
💡 Hint
Common Mistakes
Casting to a different type causes runtime errors.
Casting to object does not unbox.
Casting to string or double is incorrect here.
3fill in blank
hard

Fix the error in the code that tries to unbox an object to a double.

C Sharp (C#)
object boxedValue = 10;
double value = ([1])boxedValue;
Drag options to blanks, or click blank then click option'
Aobject
Bstring
Cdouble
Dint
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to unbox directly to a different value type causes exceptions.
Casting to string or object does not unbox.
4fill in blank
hard

Fill both blanks to create a dictionary that maps integers to their boxed objects only if the integer is greater than 5.

C Sharp (C#)
var numbers = new List<int> {3, 7, 10};
var dict = numbers.Where(n => n [2] 5).ToDictionary(n => n, n => [1]);
Drag options to blanks, or click blank then click option'
A(object)n
Bn >
C>
Dn <
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect comparison operators.
Not boxing the integer properly.
Placing the condition incorrectly.
5fill in blank
hard

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.

C Sharp (C#)
public int DoubleIfBoxedInt(object obj) {
    if (obj is [1] value) {
        return value [2] 2;
    }
    return [3];
}
Drag options to blanks, or click blank then click option'
Aint
B*
C0
Ddouble
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong type in pattern matching.
Using incorrect operator for multiplication.
Returning wrong default value.