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

Multiple generic parameters in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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> {}
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
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)
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
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
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.