I had a phenomenal day today. With a few pointers from a couple of my colleagues (Thanks Tom and Isiah), I wrote some of the best code I have ever written.

Whilst working on some Test Utility Methods, I noticed I had a few methods that were basically duplicate code with the exception of their return type and the type they used inside the method (the same type.) Previously I had only used generics in enumerating collections; Tom mentioned if I could get the conceptual “Penny to drop”, this was another example that lent itself to using generics.

My refactored code looked something similar to this:

public T SomeMethod<T>()
T myObject = AnotherMethod(typeof(T));
return myObject;