Udostępnij za pośrednictwem


Collections in the .NET Framework with Visual Basic

The .NET Framework provides specialized classes for data storage and retrieval. These classes provide support for stacks, queues, lists, and hash tables. Most collection classes implement the same interfaces. You can create your own collection classes, if needed, by implementing these interfaces.

You should to determine which type of collection is most appropriate for your needs.

Differences Between Visual Basic and .NET Framework Collection Classes

The .NET Framework collection classes are defined in the System.Collections, System.Collections.Generic, System.Collections.Specialized, and System.Collections.ObjectModel namespaces. The main differences between the Visual Basic and .NET Framework collection classes include the following:

  • Index base. .NET Framework collections are zero-based, while the Visual Basic collection is one-based. This means that the elements of a Visual Basic collection have index values from 1 through the value of the Count property, whereas the elements of a .NET Framework collection have index values from 0 through one less than the value of the collection's Count property.

  • Element type. The Visual Basic collection supports elements of type Object. This collection is not type-safe—you can add an element of any data type. This usually results in degraded performance because you must convert the elements from the Object data type to their true type.

    Some of the .NET Framework collections also have elements of type Object, but many others are strongly typed, meaning they support elements of a specific type, which makes them type-safe and usually results in optimal performance.

  • Keyed elements. The Visual Basic collection allows you to specify a key when you add an element to it. The key is a unique string value which you can use later to access that particular element. The .NET Framework collections vary in regard to keys. Some support keys and some do not.

System.Collections Classes

The classes in the System.Collections namespace do not store elements as specifically typed objects, but as objects of type Object.

The following table lists some of the commonly used classes:

Class

Description

ArrayList

Implements the IList interface, using an array whose size is dynamically increased as required.

BitArray

Manages a compact array of bit values, which are represented as Boolean values, where True indicates that the bit is on (1) and False indicates the bit is off (0).

Hashtable

Represents a collection of key/value pairs that are organized based on the hash code of the key.

Queue

Represents a first-in-first-out (FIFO) collection of objects.

Stack

Represents a simple last-in-first-out (LIFO) non-generic collection of objects.

System.Collections.Generic and System.Collections.ObjectModel Classes

The System.Collections.Generic and System.Collections.ObjectModel namespaces provide generic types, which allow you to create strongly typed collections and specify the element data type when you create them.

The following table lists some of the commonly used classes:

Class

Description

Collection<T>

Provides the base class for a generic collection.

Dictionary<TKey, TValue>

Represents a collection of key/value pairs that are organized based on the key.

KeyedCollection<TKey, TItem>

Provides the abstract base class for a collection whose keys are embedded within the values.

LinkedList<T>

Represents a doubly linked list.

LinkedListNode<T>

Represents a node in a LinkedList<T>. This class cannot be inherited.

List<T>

Implements the IList<T> interface using an array whose size is dynamically increased as required.

Queue<T>

Represents a first-in-first-out collection of objects.

SortedDictionary<TKey, TValue>

Represents a collection of key/value pairs that are sorted on the key.

SortedList<TKey, TValue>

Represents a collection of key/value pairs that are sorted by key based on the associated IComparer<T> implementation.

Stack<T>

Represents a variable size last-in-first-out (LIFO) collection of instances of the same arbitrary type.

ReadOnlyCollection<T>

Provides the base class for a generic read-only collection.

System.Collections.Specialized Classes

The System.Collections.Specialized namespace provides specialized and strongly typed collection classes, such as string-only collections and linked-list and hybrid dictionaries.

The following table lists some of the commonly used classes:

Class

Description

CollectionsUtil

Creates collections that ignore the case in strings.

HybridDictionary

Implements IDictionary by using a ListDictionary while the collection is small, and then switching to a Hashtable when the collection gets large.

ListDictionary

Implements IDictionary using a singly linked list. Recommended for collections that typically contain 10 items or less.

NameObjectCollectionBase

Provides the abstract base class for a collection of associated string keys and object values that can be accessed either with the key or with the index.

NameValueCollection

Represents a collection of associated string keys and string values that can be accessed either with the key or with the index.

OrderedDictionary

Represents a collection of key/value pairs that are ordered based on the key or index.

StringCollection

Represents a collection of strings.

StringDictionary

Implements a hash table with the key and the value strongly typed to be strings rather than objects.

See Also

Reference

Collection

Concepts

Collections in Visual Basic

Other Resources

Going Further with Visual Basic