• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Are C# non-generic collections obsolete?

 
Ranch Hand
Posts: 2949
13
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
C# has both generic and non generic collections? Are the non generic collections of C# obselette.

Thanks
 
Marshal
Posts: 79965
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would anybody want to use a non‑generic collection if generic collections are available?
 
Saloon Keeper
Posts: 15728
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2949
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15728
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suppose you could put it that way.
 
Monica Shiralkar
Ranch Hand
Posts: 2949
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.  I think like vector had gone obselette in Java.
 
Rancher
Posts: 119
8
Mac Mac OS X Safari
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2949
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
 
Ranch Hand
Posts: 82
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
c# have tow a different type of collection (1) generic  (2) non-generic
In non-generic collections, each element can represent a value of a different type.
The collection size is not fixed. Items from the collection can be added or removed at runtime.
then c# is a non-generic collection.
 
Stephan van Hulst
Saloon Keeper
Posts: 15728
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ishan Shah wrote:In non-generic collections, each element can represent a value of a different type.


This is true for generic collections as well, as you can add elements that are a subtype of the generic type argument of the collection.

The collection size is not fixed. Items from the collection can be added or removed at runtime.


Not true in general. There are both fixed size and unmodifiable collections.
 
God is a comedian playing for an audience that is afraid to laugh - Voltair. tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic