• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Are C# non-generic collections obsolete?

 
Ranch Hand
Posts: 1172
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C# has both generic and non generic collections? Are the non generic collections of C# obselette.

Thanks
 
Marshal
Posts: 65365
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would anybody want to use a non‑generic collection if generic collections are available?
 
Saloon Keeper
Posts: 10494
224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean "obsolete". That's a tough question to answer. Many APIs still work with the non-generic versions, and some have made a partial transition in that they check that a collection implements the generic version before they attempt call the non-generic methods.

I recommend that when you write methods that accept or return collections, they only accept or return the generic versions. When you write a collection type, implement BOTH the non-generic and generic versions, but give explicit method implementations for the non-generic version, so they can not be called directly on your type. You can easily do this by extending either the System.Collections.ObjectModel.Collection<T> or the System.Collections.ObjectModel.ReadOnlyCollection<T> class, which confusingly implement both the IList and the IList<T> interfaces. If you don't want your collection to be indexable, you can also just implement ICollection and ICollection<T>. Here's an example of a Heap implementation:

 
Monica Shiralkar
Ranch Hand
Posts: 1172
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.. If new generic collections have been introduced and the non generic ones are still there because the transition is still doing on then is it not the same case as saying that non generic collections are obselette now. Is there any difference.
 
Stephan van Hulst
Saloon Keeper
Posts: 10494
224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose you could put it that way.
 
Monica Shiralkar
Ranch Hand
Posts: 1172
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.  I think like vector had gone obselette in Java.
 
Rancher
Posts: 81
7
Mac Mac OS X Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Parts of the .NET framework code still use the non-generic versions. This is only for legacy reasons, or special use cases. Lots of code uses the older collections as a base class, then adds behavior on top of it.

You should always use the Generic versions. The generic versions are better, they also benefit from new extension methods that target the newer interfaces. IEnumerable<T>(the main hook into LINQ) vs IEnumerable(You use .Cast<T>() to enable to use LINQ). Don't use the older Collections unless there is a specific API that requires them for legacy reasons.  
 
Monica Shiralkar
Ranch Hand
Posts: 1172
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
If I had asked people what they wanted, they would have said faster horses - Ford. Tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!