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

Boxing and unboxing execution 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 num = 10;
object boxedNum = [1];
Drag options to blanks, or click blank then click option'
Anum.ToString()
B(object)num
CConvert.ToObject(num)
Dnum
Attempts:
3 left
💡 Hint
Common Mistakes
Using ToString() instead of boxing.
Trying to use Convert.ToObject which does not exist.
2fill in blank
medium

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

C Sharp (C#)
object boxedNum = 20;
int unboxedNum = [1];
Drag options to blanks, or click blank then click option'
A(int)boxedNum
BConvert.ToInt32(boxedNum)
CboxedNum as int
Dint.Parse(boxedNum.ToString())
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'as int' which returns null for value types.
Using Convert.ToInt32 which works but is not unboxing.
3fill in blank
hard

Fix the error in the unboxing code to avoid InvalidCastException.

C Sharp (C#)
object boxedValue = 15;
long unboxedValue = [1];
Drag options to blanks, or click blank then click option'
A(long)(int)boxedValue
B(long)boxedValue
C(int)boxedValue
DConvert.ToInt64(boxedValue)
Attempts:
3 left
💡 Hint
Common Mistakes
Casting directly to long causes InvalidCastException.
Using Convert.ToInt64 does not unbox but converts.
4fill in blank
hard

Fill both blanks to create a dictionary with boxed integers and unbox them safely.

C Sharp (C#)
var dict = new Dictionary<string, object> {
    {"one", [1],
    {"two", [2]
};
Drag options to blanks, or click blank then click option'
A1
B"two"
C2
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using string values instead of integers for values.
Using non-string keys.
5fill in blank
hard

Fill all three blanks to unbox values from the dictionary and sum them.

C Sharp (C#)
var dict = new Dictionary<string, object> {
    {"a", 5},
    {"b", 10}
};
int sum = (int)dict[[1]] + (int)dict[[2]] + [3];
Drag options to blanks, or click blank then click option'
A"a"
B"b"
C0
D15
Attempts:
3 left
💡 Hint
Common Mistakes
Using integer keys instead of string keys.
Adding a wrong number instead of zero.