Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are non-generic collections in .NET obsolete?

Put differently:

Is there a good reason to choose a loosely-typed collection over a type-safe one (HashTable vs. Dictionary)? Are they still there only for compatibility?

As far as I understand, generic collections not only are type-safe, but their performance is better.


Here's a comprehensive article on the topic: An Extensive Examination of Data Structures Using C# 2.0.

like image 653
guillermooo Avatar asked Sep 11 '08 12:09

guillermooo


People also ask

What are non-generic collections?

Non-generic collections hold elements of different datatypes. The following are the non-generic collections: ArrayList, BitArray. ArrayList − It represents ordered collection of an object that can be indexed individually. ArrayList is an alternative to an array.

What is the difference between collection and generic collection?

Examples of Collections are IEnumerable, ICollection, ArrayList, HashTable, Stack, Queue etc. Generics are similar to collections, but implemented using Type parameters. Generic collections accept a type parameter and accept the elements of only those type for which the generic collection is instantiated.

Why do we need generic collections?

A generic collection is strongly typed (you can store one type of objects into it) so that we can eliminate runtime type mismatches, it improves the performance by avoiding boxing and unboxing.

What is the difference between generic and non-generic collections in C#?

A Generic collection is a class that provides type safety without having to derive from a base collection type and implement type-specific members. A Non-generic collection is a specialized class for data storage and retrieval that provides support for stacks, queues, lists and hashtables.


2 Answers

The non-generic collections are so obsolete that they've been removed from the CoreCLR used in Silverlight and Live Mesh.

like image 158
Curt Hagenlocher Avatar answered Oct 11 '22 15:10

Curt Hagenlocher


There are also issues with COM visibility - COM interop can't be used with generics

like image 43
Tony Lee Avatar answered Oct 11 '22 13:10

Tony Lee