Reinventing The Wheel

Generic Methods and Type Inference

When you are calling a generic method, you don't have to specify the type in between the triangular brackets. The compiler can infer the type from the argument.

Ie. You don't have to use this:

Display<String>("Abc");

Instead, you can write:

Display("Abc");

Since this is the case, which method would be called in the following code segment?:

static void Main(string[] args)
{
   Display("Abc");
}

private static void Display(String s)
{
   Console.WriteLine(s);
}

private static void Display<T>(T o)
{
   Display(o.ToString());
}

The C# compiler will always choose a more explicit match over a generic match, and therefore the first method will be called. This is a good thing because if it executed the generic method, there would be an infinite recursion since the Display method calls Display().