Another good question for interviews

September 14th, 2006

Given this class declaration:

public class Some<TKey> where TKey: IComparable<TKey>
  public static int Added;

  public void Add(TKey k)

And the following usage

public class Driver
  public static void Main()
    Some<int> intSome = new Some<int>();

    Some<string> strSome = new Some<string>();


What would be the output? Just a tip: check the C# language spec.

Categories: Fun | Top Of Page | 8 Comments » |

8 Responses to “Another good question for interviews”

Aaron Says:

Wouldn’t the answer be; it doesn’t compile ?

hammett Says:

haha, sorry, the blog stripts the greater than and less than. Fixing.

Ayende Rahien Says:

The output is:

Some.Added is a different variable than Some.Added, a very useful fact that we are making use of in AR, IIRC.
This is because in runtime, a whole new tpe is being generated for each generic parameter used.

hammett Says:

Right on, Ayende!

Christopher Bennage Says:

I need to start checking your blog earlier so that I can have a shot at answering these questions. :)
I wouldn’t ask this in a interview though. When it comes to interviews I am more concerned about a developer’s approach to solving problems. I can teach Generics, or a new language even, to someone who is already a good problem solver, but teaching Good Problem Solving skills to someone is really difficult.
I am struggling with ways to discover Good Problem Solving skills in the interview process. I usually don’t know until I’ve actually worked somone a few days.

hammett Says:

Christopher, I totally agree. But this is used to measure the level of interest/knowledge the candidate has. I truly wish that the candidate doesn’t know the answer for this one, but would love to hear the rationale behind his answer. Will he/she infer that a generic type inherits static from the generic definition? Will it have different copies of the statics for each generic instantiation? That’s what I’m looking for.

During my interview at MS they asked me crazy things. For example explaining how MP3 format works, how would I implement a wireless gadget.

Eber Irigoyen Says:

a better question would’ve been to add a third variable:

Some intSome2 = new Some();

hammett Says:

Eber, agreed!

Leave a Reply