Generic method type inference in C#

I feel like an idiot - and not for the first time either! In this case it took Resharper 2.5 to point out my mistake.

I have a method that looks a bit like this:

public static void FillCollectionFromCommand<T>(IDbCommand command, ICollection<T> collection, ParseItemFromRecord<T> parser) { ... }

Basically, it calls the parser function to populate a strongly typed collection from the execution of a command.

My method calls have looked something like this:

IList<Guid> list = new List<Guid>(); Data.FillCollectionFromCommand<Guid>(command, list, getGuid);

Resharper kindly pointed out that the emphasised code was redundant, because the compile infers type T as Guid from the list parameter, which we have declared as IList<Guid>.

The new calls looks like this:

IList<Guid> list = new List<Guid>(); Data.FillCollectionFromCommand(command, list, getGuid);

This may not seem like a big deal, but it does make for much more readable code, especially when you start using types like KeyValuePair<Guid, String>, and that’s why I’m bothering telling you so*. And here’s me thinking I had to wait for C# 3.0 for type inference.

Comments