What is Collection in .NET?
By Vishal Thakur
Introduction
Microsoft .NET provides a variety of collection data types that can store a set of records or values. There are two types of collections in .NET: generic collections and non-generic collections. Generic collections are type-safe at compile time, providing better performance compared to non-generic collections. They support type parameters when constructed and do not require boxing-unboxing or type conversion from the Object type when adding or removing items. Non-generic collections store items as Objects, require casting, and are not recommended for modern development. Most developers prefer generic collections because they are faster, safer, and less prone to exceptions and compile-time errors.
To use non-generic collections, you need to include the System.Collections
namespace. For generic collections, use the System.Collections.Generic
namespace.
Generic Collections Data Types
The term Generics refers to a general type, not a specific one. Generics maximize code reuse, type safety, and performance. The most common use of generics is to create collection classes. The namespace used for generics is System.Collections.Generic
.
The letter T
represents generics and acts as an encapsulation for data types.
Example
List<T>
is a generic collection that can be declared and used with any type, such as List<int>
, List<string>
, or List<GenericClass>
.
How to Define Generics
Generics can be classes, structures, interfaces, or methods with parameters. Below is an example of a generic class:
public class GenericClass<T>
{
public T Field;
}
To instantiate a generic class or method, you need to specify the actual type. Below is an example of how to use the generic class:
public static void Main()
{
GenericClass<string> instanceObject = new GenericClass<string>();
instanceObject.Field = "This is my string";
Console.WriteLine("My Generic Field = \"{0}\"", instanceObject.Field);
Console.WriteLine("Generic.Field.GetType() = {0}", instanceObject.Field.GetType().FullName);
}
List of Generic Collections
- List<T>: Stores elements of a specified type and grows dynamically.
- Dictionary<TKey, TValue>: Stores key-value pairs.
- SortedList<TKey, TValue>: Stores key-value pairs in ascending order by key.
- Queue<T>: Stores elements in a First-In-First-Out (FIFO) manner.
- Stack<T>: Stores elements in a Last-In-First-Out (LIFO) manner.
- LinkedList<T>: Allows sequential access to items.
- ObservableCollection<T>: Notifies when items are added or removed.
- HashSet<T>: Stores unique values and eliminates duplicates.
- SortedSet<T>: A set for mathematical functions.
Non-Generic Collection Data Types
Non-generic collections are older and less recommended due to their disadvantages compared to generic collections. Below is a list of non-generic collections:
- ArrayList: Stores objects of any type dynamically.
- SortedList: Stores key-value pairs in ascending order by key.
- Stack: Stores elements in a LIFO manner.
- Queue: Stores elements in a FIFO manner.
- HashTable: Stores key-value pairs organized by hash code.
- BitArray: Stores bit values (true/false).
Features of Collections
- All collection data types provide full control to add, remove, and find values.
- They allow copying content to an array using the
CopyTo()
method. - They support enumeration using loops or LINQ expressions.
- They have
Capacity
andCount
properties to manage elements.
Conclusion
Generic collections are more efficient and safer compared to non-generic collections. It is recommended to use generic collections in modern .NET development to avoid exceptions and improve performance.
I hope this guide helps you understand collections in .NET. Please share your feedback so that I can improve my blogs. Thank you! :)
Comments
Post a Comment