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).
2 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 May 27 '09 at 16:47 @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 May 27 '09 at 16:58 Yes. It fails under certain relatively simple situations too!
:-) – Eric Lippert May 27 '09 at 20:10 @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 May 27 '09 at 20:54 1 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 May 27 '097 at 15:38.
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.
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 May 27 '09 at 16:21.
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).
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.
Thanks Reed. I didn't know that guideline. If you write a generic class, then is it still valid?
– Joan Venge May 27 '09 at 16:47 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 May 27 '09 at 17:42.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.