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

Why generics are needed in C Sharp (C#) - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Generics Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of non-generic collection usage
What is the output of this C# code that uses a non-generic ArrayList to store integers and strings?
C Sharp (C#)
using System;
using System.Collections;

class Program {
    static void Main() {
        ArrayList list = new ArrayList();
        list.Add(10);
        list.Add("hello");
        int sum = 0;
        foreach (var item in list) {
            if (item is int) {
                sum += (int)item;
            }
        }
        Console.WriteLine(sum);
    }
}
A0
Bhello
C10
DRuntime error
Attempts:
2 left
💡 Hint
Look at how the code sums only integers in the list.
Predict Output
intermediate
2:00remaining
Output of generic List usage
What is the output of this C# code that uses a generic List to store integers?
C Sharp (C#)
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        List<int> list = new List<int>();
        list.Add(10);
        list.Add(20);
        int sum = 0;
        foreach (int item in list) {
            sum += item;
        }
        Console.WriteLine(sum);
    }
}
A30
B10 20
C20
DCompilation error
Attempts:
2 left
💡 Hint
Sum all integers in the list.
🔧 Debug
advanced
2:00remaining
Why does this code cause a runtime error?
This code uses a non-generic ArrayList but throws an exception at runtime. Why?
C Sharp (C#)
using System;
using System.Collections;

class Program {
    static void Main() {
        ArrayList list = new ArrayList();
        list.Add(10);
        list.Add("hello");
        int total = 0;
        foreach (int item in list) {
            total += item;
        }
        Console.WriteLine(total);
    }
}
AIndexOutOfRangeException because of foreach
BInvalidCastException because "hello" cannot be cast to int
CNo error, output is 10
DNullReferenceException because list is null
Attempts:
2 left
💡 Hint
Check the type casting inside the foreach loop.
🧠 Conceptual
advanced
2:00remaining
Why use generics instead of non-generic collections?
Which of the following is the main reason to use generics in C# collections?
ATo avoid runtime errors by enforcing type safety at compile time
BTo make code run slower but use less memory
CTo allow storing any type without restrictions
DTo make code compatible only with older .NET versions
Attempts:
2 left
💡 Hint
Think about when errors happen with non-generic collections.
Predict Output
expert
3:00remaining
Output of generic method with type parameter
What is the output of this C# code using a generic method to print the type and value?
C Sharp (C#)
using System;

class Program {
    static void PrintValue<T>(T value) {
        Console.WriteLine($"Type: {typeof(T)}, Value: {value}");
    }

    static void Main() {
        PrintValue(123);
        PrintValue("abc");
        PrintValue(3.14);
    }
}
A
Type: T, Value: 123
Type: T, Value: abc
Type: T, Value: 3.14
BCompilation error due to generic method
C
Type: System.Object, Value: 123
Type: System.Object, Value: abc
Type: System.Object, Value: 3.14
D
Type: System.Int32, Value: 123
Type: System.String, Value: abc
Type: System.Double, Value: 3.14
Attempts:
2 left
💡 Hint
typeof(T) shows the actual type used when calling the method.