Skip to content
Sharp

Generics · interview question

What does this program print?

What does this code output?Medium
C#
Counter<int>.Count++;
Counter<int>.Count++;
Counter<string>.Count++;
Console.WriteLine($"{Counter<int>.Count} {Counter<string>.Count}");

class Counter<T>
{
    public static int Count;
}
Choose one answer
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.

Test yourself on Generics

A free test with instant, explained results. No account needed.

Start free test