Generics · interview question
What does this program print?
What does this code output?Medium
Counter<int>.Count++;
Counter<int>.Count++;
Counter<string>.Count++;
Console.WriteLine($"{Counter<int>.Count} {Counter<string>.Count}");
class Counter<T>
{
public static int Count;
}Answer and explanation
Correct answer
A. 2 1
Why
Each closed generic type (Counter<int>, Counter<string>) is a distinct runtime type with its own static fields.
References
Test yourself on Generics
A free test with instant, explained results. No account needed.