Skip to content

Latest commit

History

History
61 lines (44 loc) 路 3.58 KB

File metadata and controls

61 lines (44 loc) 路 3.58 KB

PracticalIterationAndRecursion - Iteration vs Recursion Patterns in C#

YouTube Blog Newsletter All Links LinkedIn

This project compares three different approaches to traversing a file system directory structure in C#, demonstrating the practical equivalence and trade-offs between iteration and recursion. Using a real-world file/folder hierarchy as the problem domain, each approach produces the same result through fundamentally different control flow mechanisms.

The project implements iterative depth-first traversal using a Stack, iterative breadth-first traversal using a Queue, and recursive depth-first traversal using recursive method calls. The Entry record struct holds path, name, and indentation level for each directory entry, providing clean output formatting. The recursive implementation includes helper methods like ProcessDirectoriesRecursive and ProcessFilesRecursive that demonstrate array-based recursion with index parameters.

This educational example helps developers understand when to choose iteration over recursion and vice versa, the relationship between data structures (stacks vs queues) and traversal order, and how these patterns apply to hierarchical data processing in real-world applications.

Getting Started

Prerequisites

  • .NET 8.0 SDK or later
  • An IDE such as Visual Studio, Visual Studio Code, or JetBrains Rider

Running the Project

dotnet run --project PracticalIterationAndRecursion.FileFolderExample

Related Resources

YouTube Videos

Newsletter

If you found this useful and you want to learn more about C#, .NET, and software engineering, subscribe to the free Dev Leader Weekly newsletter:

Subscribe to Dev Leader Weekly

Connect with Dev Leader


BrandGhost

Powered by BrandGhost 馃懟