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

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

Choose your learning style9 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.