System.Collections Namespace

Namespace: System.Collections

Provides interfaces and classes that define various collections of objects, such as lists, queues, hash tables, dictionaries, and sets.

Namespace: System.Collections

Assembly: System.Runtime (in System.Runtime.dll)

Classes

ArrayList class

Represents a strongly typed list of objects that can be accessed by index. Provides methods for creating, searching and sorting collections.

Queue class

Represents a strongly typed collection of objects that are accessed by index. Provides methods for creating, searching and sorting collections.

Stack class

Represents a collection of objects that can be accessed by index. Provides methods for creating, searching and sorting collections.

Hashtable class

Represents a collection of key and value pairs that are organized by the hash code of the key. This class cannot be inherited.

SortedList class

Represents a collection of key and value pairs that are sorted on the key. Each element is a key/value pair.

DictionaryBase class

Provides the base class for implementing a collection that contains entries identified by keys.

Interfaces

ICollection interface

Implements the basic contract for collections of objects.

IEnumerable interface

Represents the enumerator for a collection.

IEnumerator interface

Supports a simple iteration over a non-generic collection.

IDictionary interface

Represents a collection of entries, each of which has a unique key.

IList interface

Represents a non-generic collection of objects that can be individually accessed by index.

Enums

DictionaryEntry struct

Represents a key and value pair that can be set or retrieved.

Usage Example

Here's a basic example of using an ArrayList to store and iterate through items:

using System.Collections; using System; public class Example { public static void Main() { ArrayList myArrayList = new ArrayList(); myArrayList.Add("Hello"); myArrayList.Add("World"); myArrayList.Add(123); Console.WriteLine("Items in ArrayList:"); foreach (object item in myArrayList) { Console.WriteLine(item); } } }

This code demonstrates how to add elements of different types to an ArrayList and then iterate through them using a foreach loop.