What does it mean to have multiple generic parameters in C#?
It means a class, method, or interface can use more than one placeholder type, allowing it to work with different types at the same time.
Click to reveal answer
beginner
How do you declare a class with two generic parameters in C#?
You write the class name followed by angle brackets with two type names separated by a comma, like: <T1, T2>.
Click to reveal answer
intermediate
Example: What is the correct syntax for a method with two generic parameters?
public void Swap(ref T1 a, ref T2 b) { /* method body */ }
Click to reveal answer
beginner
Why use multiple generic parameters instead of just one?
Because sometimes you want to work with two or more different types at once, like pairing a key and a value in a dictionary.
Click to reveal answer
intermediate
Can generic parameters have constraints when using multiple generic parameters?
Yes, each generic parameter can have its own constraints to limit what types can be used.
Click to reveal answer
How do you declare a generic class with two type parameters in C#?
Aclass MyClass<T1, T2> {}
Bclass MyClass<T1; T2> {}
Cclass MyClass<T1 T2> {}
Dclass MyClass<T1 & T2> {}
✗ Incorrect
The correct syntax uses commas to separate multiple generic parameters inside angle brackets.
What is the benefit of using multiple generic parameters?
ATo avoid using any types
BTo make code slower
CTo restrict code to only one type
DTo work with multiple different types in one class or method
✗ Incorrect
Multiple generic parameters allow handling different types flexibly in the same code.
Which of these is a valid method signature with two generic parameters?
Avoid Combine<T1 T2>(T1 a, T2 b)
Bvoid Combine<T1; T2>(T1 a, T2 b)
Cvoid Combine<T1, T2>(T1 a, T2 b)
Dvoid Combine<T1 & T2>(T1 a, T2 b)
✗ Incorrect
Generic parameters must be separated by commas inside angle brackets.
Can each generic parameter have its own constraints?
ANo, constraints apply to all parameters together
BYes, each can have different constraints
COnly one parameter can have constraints
DConstraints are not allowed on generic parameters
✗ Incorrect
Each generic parameter can have its own constraints to specify allowed types.
What does this declaration mean? class Pair {}
AA class that works with two types, T1 and T2
BA class that only works with one type
CA method with two parameters
DAn error in syntax
✗ Incorrect
This declares a generic class with two type placeholders, T1 and T2.
Explain how to declare and use a class with multiple generic parameters in C#.
Think about how you write a generic class with one parameter, then add another separated by a comma.
You got /3 concepts.
Describe the benefits and use cases of multiple generic parameters.
Consider why a dictionary needs two types: one for key, one for value.
You got /3 concepts.
Practice
(1/5)
1.
What is the correct way to declare a generic class with two type parameters in C#?
class MyClass<T1, T2> { }
easy
A. class MyClass<T1> { }
B. class MyClass<T1, T2> { }
C. class MyClass<T1; T2> { }
D. class MyClass<T1 T2> { }
Solution
Step 1: Understand generic class syntax
In C#, multiple generic parameters are separated by commas inside angle brackets <>.
Step 2: Check each option's syntax
class MyClass<T1, T2> { } correctly uses a comma between T1 and T2. Other options use a single parameter, semicolon, or space which are incorrect.
Final Answer:
class MyClass<T1, T2> { } -> Option B
Quick Check:
Multiple generic parameters use commas inside <> [OK]
Hint: Separate generic types with commas inside angle brackets [OK]
Common Mistakes:
Using semicolons or spaces instead of commas
Forgetting to separate multiple generic parameters
Declaring only one generic parameter when two are needed
2.
Which of the following is the correct syntax to declare a method with two generic parameters in C#?
public void Swap<T1, T2>(T1 a, T2 b) { }
easy
A. public void Swap<T1 T2>(T1 a, T2 b) { }
B. public void Swap<T1; T2>(T1 a, T2 b) { }
C. public void Swap<T1, T2>(T1 a, T2 b) { }
D. public void Swap(T1 a, T2 b) { }
Solution
Step 1: Recall method generic parameter syntax
Generic parameters for methods are declared inside angle brackets <> separated by commas.
Step 2: Evaluate each option
public void Swap<T1, T2>(T1 a, T2 b) { } correctly uses commas between T1 and T2. Other options use invalid separators like space or semicolon, or miss the generic parameters entirely.
Final Answer:
public void Swap<T1, T2>(T1 a, T2 b) { } -> Option C
Quick Check:
Method generics need commas inside <> [OK]
Hint: Use commas to separate generic parameters in method declarations [OK]
Common Mistakes:
Omitting generic parameters in method signature
Using spaces or semicolons instead of commas
Placing generic parameters outside angle brackets
3.
What will be the output of the following C# code?
class Pair<T1, T2> {
public T1 First;
public T2 Second;
public Pair(T1 first, T2 second) {
First = first;
Second = second;
}
public override string ToString() {
return $"{First} and {Second}";
}
}
var p = new Pair<int, string>(5, "hello");
Console.WriteLine(p);
medium
A. 5, hello
B. Pair`2
C. System.Pair
D. 5 and hello
Solution
Step 1: Understand the Pair class and ToString override
The Pair class stores two values and overrides ToString to return "{First} and {Second}".
Step 2: Analyze the instantiation and output
We create Pair<int, string> with 5 and "hello". Console.WriteLine calls ToString, so output is "5 and hello".
Final Answer:
5 and hello -> Option D
Quick Check:
ToString returns "{First} and {Second}" [OK]
Hint: Check ToString override for output format [OK]
Common Mistakes:
Assuming default ToString output for generic class
Confusing comma with 'and' in output
Ignoring ToString override
4.
Identify the error in the following code snippet:
class Container<T1, T2> {
public T1 Item1;
public T2 Item2;
public Container(T1 item1, T2 item2) {
Item1 = item1;
Item2 = item2;
}
}
var c = new Container<int>(5, 10);
medium
A. Missing second generic type argument when creating Container instance
B. Constructor parameters types do not match generic types
C. Cannot use int as generic type
D. Missing semicolon after class declaration
Solution
Step 1: Check generic parameters in class and instance
The class Container requires two generic types: T1 and T2.
Step 2: Analyze the instance creation
The code uses Container<int> with only one generic argument, but two are required.
Final Answer:
Missing second generic type argument when creating Container instance -> Option A
Quick Check:
Two generic parameters require two type arguments [OK]
Hint: Match number of generic arguments with parameters [OK]
Common Mistakes:
Providing fewer generic arguments than declared
Assuming default generic types
Ignoring compiler errors about generic arguments
5.
Given the following generic method, what is the correct way to call it with two different types?