tags:

views:

290

answers:

4

When you have a method like:

public static T DoSomething<T> ( params T [ ] input )

C# lets you to call it without specifying the T, like:

DoClass.DoSomething ( "1", "2", "3" );

Does the compiler figure out T by what's passed to it?

Is this a good convention (to leave out T in this case)?

+3  A: 

Yes, the compiler can infer the generic type parameter in the majority of cases. (One exceptin being when your type is a lambda expression, if I remember right.)

It is generally considered perfectly good practice to omit the generic parameters when they can be inferred. In fact, I would say that it increases readability a certain amount (specifying them is often quite redundant).

Noldorin
The algorithm for inferring types from lambdas is complex. See the "second phase" section of the specification for details on how we do so.
Eric Lippert
@Eric: Yeah, I would certainly imagine so. Without reading it full, I am right in saying that it fails under certain complicated situations, no?
Noldorin
Yes. It fails under certain relatively simple situations too! :-)
Eric Lippert
@Eric: Yeah, that's the impression I've gotten from using them, though I haven't quite figured out the exact conditions yet! Would probably be interesting to read the language spec on this.
Noldorin
Indeed, though if you do, there are a few small errors in the spec regarding the exact operation of phase two in some error cases. Eventually I'll blog about what the specification should say.
Eric Lippert
Also, if you're interested in just the basic scenarios of how this works with lambdas, here's a video I did a few years ago on how it works: http://blogs.msdn.com/ericlippert/archive/2006/11/17/a-face-made-for-email-part-three.aspx
Eric Lippert
Thanks for that, Eric. I'll keep an eye out for that blog post too. :)
Noldorin
+1  A: 

Yes, the compiler will typically figure out the type. It's called "type inference".

And yes, it's a best practice to leave T out at the call site. The less code you write, the less code someone has to read and understand later.

If you have ReSharper, it will do a good job of showing you where you can and can't get away with removing the at the call site. Otherwise you can try taking it out, and if the code compiles, then you didn't need it.

Joe White
More specifically, it's called "generic parameter type inference". There also exist "local parameter type inference" and "lambda expression type inference" in C# 3.0, which are similar but distinct.
Noldorin
+1  A: 

Yes, when the compiler can figure out what T is supposed to be, it is redundant to specify it. I find this a great feature, because it gets tedious and hard to read when the type name is always listed (especially with long names).

TheSean
+1  A: 

As many people have mentioned, this is due to generic parameter type inference by the compiler. It discovers the type directly by the first parameter.

One other thing - if you read the design guidelines for .net libraries, it's actually recommended to write ALL of your generic methods in a way that the types can be inferred. Non-inferrable generic methods are considered more difficult to understand, and should be avoided when possible, according to the design guidelines book.

Reed Copsey
Thanks Reed. I didn't know that guideline. If you write a generic class, then is it still valid?
Joan Venge
Typically, the methods in a generic class use the same type as the class, so it isn't really an issue. It was really there for a generic methods.
Reed Copsey