Bird
Raised Fist0
C Sharp (C#)programming~10 mins

Multiple generic parameters in C Sharp (C#) - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Multiple generic parameters
Define class with <T1, T2>
↓
Create instance with types
↓
Use T1 and T2 in methods
↓
Access properties of T1 and T2
↓
Output results using both types
This flow shows how a class with two generic types is defined, instantiated with specific types, and used to access and output values of both types.
Execution Sample
C Sharp (C#)
class Pair<T1, T2> {
  public T1 First;
  public T2 Second;
  public Pair(T1 first, T2 second) {
    First = first;
    Second = second;
  }
}

var p = new Pair<int, string>(10, "hello");
Console.WriteLine($"{p.First}, {p.Second}");
Defines a generic Pair class with two types, creates an instance with int and string, then prints the values.
Execution Table
StepActionT1 ValueT2 ValueOutput
1Define class Pair<T1, T2>---
2Create instance Pair<int, string>(10, "hello")10hello-
3Assign First = 1010--
4Assign Second = "hello"-hello-
5Print values10hello10, hello
6End of program---
💡 Program ends after printing the values of both generic parameters.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
p.First--101010
p.Second---hellohello
Key Moments - 2 Insights
Why do we use two generic parameters instead of one?
Using two generic parameters lets us store and work with two different types independently, as shown in steps 2-4 where First and Second hold different types.
How does the compiler know what types T1 and T2 are?
When creating the instance (step 2), we specify the types explicitly (int and string), so the compiler replaces T1 with int and T2 with string.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 3, what is the value of p.First?
A10
B"hello"
Cnull
Dundefined
💡 Hint
Check the 'T1 Value' column at step 3 in the execution table.
At which step does the program output the values of both generic parameters?
AStep 4
BStep 5
CStep 2
DStep 6
💡 Hint
Look for the 'Output' column in the execution table.
If we changed the instance to Pair<double, bool>(3.14, true), what would p.Second be after step 4?
A3.14
Bfalse
Ctrue
Dnull
💡 Hint
Refer to variable_tracker and how Second holds the second generic type value.
Concept Snapshot
Multiple generic parameters allow a class or method to work with two or more types.
Syntax: class ClassName<T1, T2> { ... }
Create instance: new ClassName<Type1, Type2>(...)
Each generic parameter acts independently.
Use to store or process different types together.
Full Transcript
This example shows how to define a class with two generic parameters T1 and T2. We create an instance of this class with int and string types. The variables First and Second hold values of these types. The program prints both values. The execution table traces each step: defining the class, creating the instance, assigning values, and printing output. The variable tracker shows how p.First and p.Second change. Key moments clarify why two generics are useful and how the compiler knows the types. The quiz tests understanding of values at different steps and how changing types affects variables.

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

  1. Step 1: Understand generic class syntax

    In C#, multiple generic parameters are separated by commas inside angle brackets <>.
  2. 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.
  3. Final Answer:

    class MyClass<T1, T2> { } -> Option B
  4. 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

  1. Step 1: Recall method generic parameter syntax

    Generic parameters for methods are declared inside angle brackets <> separated by commas.
  2. 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.
  3. Final Answer:

    public void Swap<T1, T2>(T1 a, T2 b) { } -> Option C
  4. 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

  1. Step 1: Understand the Pair class and ToString override

    The Pair class stores two values and overrides ToString to return "{First} and {Second}".
  2. 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".
  3. Final Answer:

    5 and hello -> Option D
  4. 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

  1. Step 1: Check generic parameters in class and instance

    The class Container requires two generic types: T1 and T2.
  2. Step 2: Analyze the instance creation

    The code uses Container<int> with only one generic argument, but two are required.
  3. Final Answer:

    Missing second generic type argument when creating Container instance -> Option A
  4. 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?

public static void PrintPair<T1, T2>(T1 first, T2 second) {
    Console.WriteLine($"First: {first}, Second: {second}");
}

Choose the correct call:

hard
A. PrintPair<int, string>(10, "test");
B. PrintPair<int>(10, "test");
C. PrintPair<T1, T2>(10, "test");
D. PrintPair<string, int>(10, "test");

Solution

  1. Step 1: Understand generic method call syntax

    When calling a generic method with multiple parameters, specify all types inside <> in order.
  2. Step 2: Match types with arguments

    The method expects T1 then T2. PrintPair<int, string>(10, "test"); uses <int, string> matching arguments 10 (int) and "test" (string). PrintPair<string, int>(10, "test"); reverses types incorrectly.
  3. Final Answer:

    PrintPair<int, string>(10, "test"); -> Option A
  4. Quick Check:

    Generic method call types match argument order [OK]
Hint: Specify all generic types in order when calling method [OK]
Common Mistakes:
  • Providing fewer generic types than parameters
  • Reversing generic types order
  • Omitting generic types when required
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard