Visual Basic .NET - Collections Framework in VB.NET
The Collections Framework in VB.NET is a set of classes that help you store, manage, and organize groups of data efficiently. Instead of using simple arrays, collections provide more flexibility, better performance, and many built-in methods for adding, removing, and searching data.
In real-world programming, applications often need to handle multiple values such as lists of students, product records, or customer details. Collections make it easier to work with such grouped data.
Why Collections Are Important
Arrays have fixed sizes, which means once you declare their size, you cannot easily change it. Collections solve this problem because they can grow or shrink dynamically. They also provide useful methods like:
-
Add()
-
Remove()
-
Contains()
-
Sort()
-
Clear()
These built-in methods reduce coding effort and improve readability.
Common Collection Classes in VB.NET
1. List(Of T)
List is one of the most commonly used collections. It stores elements in order and allows duplicate values.
Example:
Dim numbers As New List(Of Integer)
numbers.Add(10)
numbers.Add(20)
numbers.Add(30)
Features:
-
Dynamic size
-
Access elements using index
-
Allows duplicates
-
Strongly typed using Generics
2. Dictionary(Of TKey, TValue)
Dictionary stores data in key-value pairs. Each key must be unique.
Example:
Dim students As New Dictionary(Of Integer, String)
students.Add(1, "Rahul")
students.Add(2, "Anita")
Features:
-
Fast searching using keys
-
Unique keys
-
Useful for lookup operations
3. Queue(Of T)
Queue follows FIFO (First In, First Out) principle.
Example:
Dim queue As New Queue(Of String)
queue.Enqueue("Task1")
queue.Enqueue("Task2")
queue.Dequeue()
Used in:
-
Task scheduling
-
Print queue systems
4. Stack(Of T)
Stack follows LIFO (Last In, First Out) principle.
Example:
Dim stack As New Stack(Of Integer)
stack.Push(5)
stack.Push(10)
stack.Pop()
Used in:
-
Undo/Redo features
-
Expression evaluation
5. HashSet(Of T)
HashSet stores unique elements only.
Example:
Dim setData As New HashSet(Of Integer)
setData.Add(10)
setData.Add(10)
Duplicate values are not stored.
Generics in Collections
Most modern collections use Generics, written as (Of T). Generics provide:
-
Type safety
-
Better performance
-
No need for type casting
Example:
Dim names As New List(Of String)
Here, only String values can be stored.
When to Use Which Collection
-
Use List when order matters and duplicates are allowed.
-
Use Dictionary when you need fast searching with unique keys.
-
Use Queue for sequential processing.
-
Use Stack for reverse order processing.
-
Use HashSet when you need unique values only.
Conclusion
The Collections Framework in VB.NET is essential for building efficient and scalable applications. It provides flexible, dynamic data structures that make handling grouped data easier and more powerful than using traditional arrays. Understanding collections is very important for real-world software development and advanced programming concepts.