Friday, February 8, 2008

Quiz 1: Generic class with static fields

In the following piece of code you can see 2 instantiations of a generic class. What is the output of this console application?

using System;
using System.Collections.Generic;


namespace DEMO
{
class Program
{
static void Main(string[] args)
{
GenClass
<int> x = new GenClass<int>();
GenClass
<double> y = new GenClass<double>();

x.Add(
1);
y.Add(
2);

Console.WriteLine(y.Get(
0));
}

public class GenClass<T>
{
public static List<T> list = new List<T>();

public void Add(T x)
{
list.Add(x);
}

public string Get(int index)
{
return list[index].ToString();
}
}
}
}


I'll post the response in a comment.