Leverage collections in .Net to store to promote type safety, facilitate code reuse, and improve performance when working with in-memory collection of objects Collections are used for storage and retrieval of data. You use collections in your application to allocate memory dynamically to store elements and then retrieve them using key or index as and when needed. Basically a collection represents a set of objects that you can access by iterating each of the elements of the collection. Microsoft .Net framework provides excellent support for working with collections. You can take advantage of the types contained in the System.Collections, System.Collections.Generic, System.Collections.Concurrent, System.Collections.Immutable namespaces to work with collections. Generic and non–generic collections There are two types of collections in .Net: generic collections and non-generic collections. Generic collections are those that provide type safety and hence better performance as you don’t have the boxing and un-boxing overheads when storing and retrieving elements to and from generic collections. You can leverage generics to enforce type safety and improve performance when working with an in-memory collection of data as there is no boxing and unboxing overhead involved. When you use generics, the compiler makes compile time checks on your code for conformance to type safety. Note that type safe collections or strongly collections are those that contain elements of a pre-defined or known type. While the standard collections are contained in the System.Collections namespace, the generic collections are contained in the System.Collections.Generic namespace. Note that you can leverage generics to create type safe collections for improved performance when working with collections. Some of the important classes in the System.Collections.Generic namsepace include: List, Queue, HashSet, LinkedList, Stack, LinkedListNode and Dictionary. Types inside the System.Collections namespace When working with collections you would need to take advantage of the System.Collections namespace. This contains a collection of classes, interfaces, methods and properties that you can make use of to work with different collections. The types in the System.Collections namespace store data as objects of type Object. The following is the list of some of the important interfaces that are contained in the System.Collections namespace. IEnumerable — this interface exposes an IEnumerator that can used to iterate a non-generic collection using a foreach loop IEnumerator — this is used to iterate a non-generic collection ICollection — this interface extends the IEnumerable interface and defines enumerators and synchronization methods for non-generic collections IList — this interface represents a non-generic ordered collection that can be accessed using index IDictionary — this interface is used to represent a non-generic collection of objects that are organized as key / value pairs And, here’s the list of the important classes in the System.Collections namespace. ArrayList — this implements the IList interface and represents a collection whole size can increase dynamically and can store objects of any type. Data inside an ArrayList instance can be accessed using the index. Hashtable — this represents a collection that can store objects as key / value pairs BitArray — this represents a collection that can manage a compact array of bit values where the values are represented as Booleans Queue — this represents a first –in first – out non – generic collection of objects Stack — this represents a last –in last – out non – generic collection of objects SortedList — this represents a collection of objects stored as key / value pairs that are sorted by the keys. Objects in a sorted list are accessible both using their keys and indexes. Note that while the ICollection interface extends the IEnumerable interface, both the IDictionary and IList interfaces extend the ICollection interface. You also have the DictionaryEntry structure in this namespace. You can use the DictionaryEntry structure for iterating through Hashtable instances. On the contrary, the KeyValuePair structure can be used to iterate items in a Dictionary (a Dictionary is also known as associative array) instance. The System.Collections.Concurrent namespace With .Net Framework 4 the collection types in the System.Collections.Concurrent namespace provide an excellent support for thread safety while accessing data from the collection type instances using multiple threads concurrently. Synchronization is a process that is used to prevent multiple threads from accessing a shared resource concurrently — you can take advantage of thread synchronization to restrict access to a shared resource. Some of the important thread-safe collection classes in the System.Collections. Generic namespace include: BlockingCollection, ConcurrentQueue, ConcurrentStack and ConcurrentDictionary. Related content feature 14 great preprocessors for developers who love to code Sometimes it seems like the rules of programming are designed to make coding a chore. Here are 14 ways preprocessors can help make software development fun again. By Peter Wayner Nov 18, 2024 10 mins Development Tools Software Development feature Designing the APIs that accidentally power businesses Well-designed APIs, even those often-neglected internal APIs, make developers more productive and businesses more agile. By Jean Yang Nov 18, 2024 6 mins APIs Software Development news Spin 3.0 supports polyglot development using Wasm components Fermyon’s open source framework for building server-side WebAssembly apps allows developers to compose apps from components created with different languages. By Paul Krill Nov 18, 2024 2 mins Microservices Serverless Computing Development Libraries and Frameworks news Go language evolving for future hardware, AI workloads The Go team is working to adapt Go to large multicore systems, the latest hardware instructions, and the needs of developers of large-scale AI systems. By Paul Krill Nov 15, 2024 3 mins Google Go Generative AI Programming Languages Resources Videos