Generics & Specialization

time to read 5 min | 925 words

Here is a proof that I didn't do C++ in too long, I completely forgot about template specialization.  Pierre-Andre van Leeuwen has a post that reminded me that such things exists.

The following code works as expected:

static void Main (string[] args)

{

    Save(new object());

    Save(new Dictionary<string, string>());

}

 

static void Save<T>(T item)

{

    Console.WriteLine("Generic method called");

}

 

static void Save(Dictionary<string, string> item)

{

    Console.WriteLine("Dictionary method called");

}

But isn't this losing any value that can be had from generics? I don't see any difference between using T and using object in this case.