Monday, January 3, 2011

Type Argument Specification is Redundant

One of the things I like about ReSharper is that it teaches me things I didn’t know.  Recently I was writing a property that calls a generic helper method that I’d created and ReSharper gave me the little popup below letting me know that I might want to refactor that line of code.


“Type argument specification is redundant”,  what the heck does that mean?  So I looked into it and learned something interesting that I didn’t know before.  To understand what’s going on let’s first look at my NewIfNull() method. 

NewIfNull() is a simple generic helper method that takes an object of any type that has a parameterless constructor, it then checks to see if that object is null.  If it is null, the helper returns a new instance of that object’s type.  If the object isn’t null, it just returns the object.  It’s kind of like lazy loading for type instantiation.  It’s useful in situations where I have properties for things like data services that I don’t want to instantiate until the first time I call them (if they’re called at all).  Here’s the NewIfNull() code:


You can see that NewIfNull() is a generic method that takes a type parameter T which represents the type of the object that I’m passing in.  A typical call to NewIfNull looks like  


This isn’t too bad, but it’s a little annoying that I have to pass _workflowService as a parameter and then I also have to specify WorkflowService as my type inside of the angle brackets.  Well apparently that part is completely unnecessary. That little helper message from ReSharper was trying to tell me that since T is just the type of my parameter obj,  .Net can infer the type of T from parameter obj.  Passing type T in the angle brackets is actually redundant. Brilliant!  Now my calls to NewIfNull() look like this.


Much cleaner. No angle brackets or generic type parameters needed.  I’ve been writing and using generic methods for quite a while and I never knew .Net would infer types like this.  Thank you ReSharper.  If you want to learn more, the MSDN documentation on Type Inference is pretty good.  Check out Section 26.3.3 Type Inference.

No comments:

Post a Comment