Should type be evident with generic method that returns its own only parameter?

We have "use var when evident" set on everything, since I prefer to use "var" when I can look at the line of code and immediately determine what the type will be.  If you new it up on that line, it's obvious what it will be.  If you cast is using an explicit conversion or with an "as", it's obvious what it will be.  However, one behavior that I'm a bit confused by is this one:

class Class1
{
public void Test()
{
var i = GetGeneric<int>();
}

public T GetGeneric<T>()
{
return default(T);
}
}

 

It appears that reSharper believes the type of "i" would be evident since GetGeneric returns a type T, which we specify as an int when we call the method.  However, this would not be evident to someone who cannot see the implementation of the method.  That type parameter might be used for something else or not even used at all.

From what I can tell, this is not a reSharper bug but a well thought-out design and quite purposeful.  For example, if I change the return type of GetGeneric to anything but T, reSharper will suggest I get rid of the var.  If I have more than one type parameter on GetGeneric, same thing.  So, one could argue that the fact I don't see a warning tells me that this method will return an int, but I would have to be well aware of reSharper behavior and my current configuration to know that.

In my opinion, the type is not "evident" and thus reSharper should not treat this as such.  I'm curious on other opinions, or perhaps the thought process behind this design decision.  Thanks!

1 comment
Comment actions Permalink

Unfortunately, because of this rule, I have to make everything to "always use explicit type", because it violates my teams coding style to have to know the implementation of a method to know what type a variable is.

 

This needs to be configurable so that this doesn't count as "evident"

0

Please sign in to leave a comment.