Algorithms and Data Structures in C#: A Practical Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Algorithms and Data Structures in C#: From Theory to Practice



Understanding algorithms and data structures is the foundation of professional C# development. Whether you are writing a high-load web application in ASP.NET Core or a desktop application in WPF, choosing the right data structure and efficient algorithm directly impacts code performance, readability, and scalability. In this article, we will break down key data structures and algorithms using specific C# code examples that you can immediately apply in your projects.



1. Stack and Queue in C#



Stack (LIFO — Last In, First Out) and Queue (FIFO — First In, First Out) are basic data structures built into .NET and widely used in real-world tasks: from processing method calls to task management.



Stack in C#

A stack is often used to implement undo actions or syntax parsing. In C#, the stack is represented by the Stack<T> class.

using System;using System.Collections.Generic;

public class StackExample{ public static void Main() { Stack<string> browserHistory = new Stack<string>(); // Adding elements (Push) browserHistory.Push("Home Page"); browserHistory.Push("News"); browserHistory.Push("Article about C#"); Console.WriteLine($"Last page: {browserHistory.Peek()}"); // Article about C# // Removing and returning the top element (Pop) string lastPage = browserHistory.Pop(); Console.WriteLine($"Going back from: {lastPage}"); // Article about C# Console.WriteLine($"Current page: {browserHistory.Peek()}"); // News }}


Queue in C#

A queue is ideal for processing tasks in the order they arrive (e.g., a print queue or message processing). Use the Queue<T> class.

using System;using System.Collections.Generic;

public class QueueExample{ public static void Main() { Queue<string> printQueue = new Queue<string>(); // Adding to the end of the queue (Enqueue) printQueue.Enqueue("Document1.pdf"); printQueue.Enqueue("Photo.png"); printQueue.Enqueue("Report.docx"); Console.WriteLine($"First to print: {printQueue.Peek()}"); // Document1.pdf // Removing from the front of the queue (Dequeue) string firstJob = printQueue.Dequeue(); Console.WriteLine($"Printing: {firstJob}"); // Document1.pdf Console.WriteLine($"Next: {printQueue.Peek()}"); // Photo.png }}


Important: .NET also has ConcurrentQueue<T> for multithreaded scenarios, and Stack<T> does not support index-based access — only the top element.



2. Array Sorting: QuickSort



Although C# has a built-in Array.Sort() method, understanding sorting algorithms is necessary for interviews and optimization. QuickSort is one of the most efficient algorithms with an average-case complexity of O(n log n).



using System;

public class QuickSortExample{ public static void QuickSort(int[] arr, int left, int right) { if (left < right) { int pivotIndex = Partition(arr, left, right); QuickSort(arr, left, pivotIndex - 1); QuickSort(arr, pivotIndex + 1, right); } }

private static int Partition(int[] arr, int left, int right) { int pivot = arr[right]; // pivot element — last one int i = left - 1;

for (int j = left; j < right; j++) { if (arr[j] < pivot) { i++;

Blogs

Book Recommendations