tags:

views:

137

answers:

4

I am reading existing posts on Generics at SO. If Generics has so many advantages like Type safety, no overhead of boxing/unboxing and it is fast, why not always use it. Why to go for a non-generic thing?

Edited (Question further extended below)

I am a bit confused. The last time I read about Generics, months back, I read that if the Type in the parameters is not sure, Generic should be used to prevent error.

But what I read now, it says that Generic limits to a fixed Type. A non-generic thing allows to define parameter type at run-time.

Please correct me.

Secondly, these things like OOP, Generics etc. are helpful when you are working in a team and your code is shareable. For a lone programmer, who knows what Type has to come in the parameter, I guess there is no need to worry, whether it is Generic or Non-Generic.

+2  A: 

You should use generics. Much of the code in the .NET framework that doesn't use generics but should was written before generics was introduced in .NET 2.0. If that code were rewritten today it probably would use generics.

Of course you do not want to use generics when you know the specific types involved. Not everything should be made generic.

Mark Byers
+7  A: 

In general you should - however, there are times (especially when writing library code) when it is not possible (or certainly not convenient) to know about the calling type (even in terms of T), so non-generic types (mainly interfaces such as IList, IEnumerable) are very useful. Data-binding is a good example of this. Indeed, anything that involves reflection is generally made much harder by using generics, and since (via the nature of reflection) you've already lost those benefits, you may as well just drop to non-generic code. Reflection and generics are not very good friends at all.

Marc Gravell
@Marc: I edited the question further. Have u seen that?
RPK
+1  A: 

Generics may be fast and type safe, but also add complexity (another dimension which can vary and must be understood by programmers). Who is going to maintain your code? Not every trick with generics (or lambdas, or dependency injection, or...) is worth it. Think about what problem you are going to solve, and what parts of that problem may change in the future. Design for those cases. The optimally flexible software is too complex to be maintained by mortal programmers.

Pontus Gagge
Some valid *general* points, but I'm not sure I can see how generics adds much complexity here... indeed, in many ways it is harder to get it wrong with generics (the compiler will whine at you).
Marc Gravell
Marc, your context isn't exactly crystal clear. Adding a design element always adds complexity. Another layer of indirection, for example, doesn't in itself add a lot of complexity, but it all adds up. The question is whether added elements are worth it. Getting generics wrong while writing code is one thing: understanding code you have to maintain is quite another. If you have a business layer Order class, in what cases would you make it generic? For what parameters? The answer, unfortunately, is: it **depends**!
Pontus Gagge
+2  A: 

Sometimes "object" collections are simply unavoidable. Often this happens when there's multiple types in the same control/collection - the only type they have in common is "object", and so that's the best type for your collection.

Another case for object (non collection related) that pops up from time to time can be seen with the PropertyGrid. A third party property grid may allow you to attach a "validator" which returns whether the users new value for a given property on the grid is acceptable. As the PropertyGrid does not know what properties it will be displaying, the best it can give the validator is an object - even though the validator knows exactly what type it will be called with.

But as per Mark's answer - most (all?) of the non generic collections in .NET are only there for legacy reasons. If .NET was remade today you can be sure the standard library would look very different.

Mania
@Mania @Mark: My long time doubt cleared. It's good to know that non-generic things are for legacy reasons only.
RPK