I hope you must have gone through my previous articles on DSA and now you understand that learning DSA is crucial to becoming a good software developer. DSA is equally important for cracking coding interviews, building scalable software for yourself or for your clients, or optimizing performance-critical systems. In C#, we already have some built-in data structures; however, with the help of available .NET classes and our custom logic, we as programmers find a better way to structure the data fetched from database, text file, or some other places. Thus we can say data structures provide us a convenient way to store data during program execution. For the next couple of articles, we will explore different data structures in C#.
Built-In Data Structures in C#
You must be familiar with the term namespaces; if not, then you can think of a namespace as a logical container that contains the same kind of things. In programming terms, a namespace is a project that contains all the similar kinds of classes, and thus, by referencing that project (namespace), you can start using the methods available under those classes. In .NET System.Collections and System.Collections.Generic are those namespaces that hold the commonly used data structures. Here are some of the most commonly used:
1.Arrays
Arrays are nothing but a data structure used to store the same kind of elements. The only disadvantage of array is its fixed size, meaning you cannot increase the size of array at run time; that implies that if more number of elements be tried to be insert than the defined size, then c# compiler will throw error.
int[] numbers = new int[5] { 1, 2, 3, 4, 5 };
2. List<T>
The generic List<T> is nothing but an upgrade of arrays and you can think of it as a dynamic array that resizes automatically
List<string> names = new List<string>();
names.Add("Alice");
names.Add("Bob");
3. Dictionary<TKey, TValue>
Both the array and List were index based, meaning the only way to find a value inside them is using their index, but sometimes we need to get the value using the key name for convenience, so .NET provides a hash table implementation for fast key-value lookup, know as dictionary.
Dictionary<string, int> ages = new Dictionary<string, int>();
ages["Alice"] = 30;
ages["Bob"] = 25;
4. Stack<T> and Queue<T>
Stacks use LIFO (Last-In, First-Out), and Queues use FIFO (First-In, First-Out) principles.
Stack<int> stack = new Stack<int>();
stack.Push(1);
stack.Push(2);
int top = stack.Pop();
Queue<int> queue = new Queue<int>();
queue.Enqueue(1);
queue.Enqueue(2);
int first = queue.Dequeue();
Custom Data Structures in C#
Sometimes, built-in structures are not sufficient and we need to build our own, such as a Linked List or a binary tree/p>
Example: Singly Linked List in C#
public class Node {
public int Data;
public Node Next;
public Node(int data) {
this.Data = data;
this.Next = null;
}
}
public class LinkedList {
private Node head;
public void Add(int data) {
Node newNode = new Node(data);
if (head == null)
head = newNode;
else {
Node current = head;
while (current.Next != null)
current = current.Next;
current.Next = newNode;
}
}
public void PrintAll() {
Node current = head;
while (current != null) {
Console.WriteLine(current.Data);
current = current.Next;
}
}
}
I know the explanations are very short and not clear, but don't worry, we will elaborate on each of the built-in data structures in our next article. We will also learn to create custom data structures.