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.
- .NET 8.0 SDK or later
- An IDE such as Visual Studio, Visual Studio Code, or JetBrains Rider
dotnet run --project PracticalIterationAndRecursion.FileFolderExampleIf 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
- All Links
- Website - Dev Leader
- YouTube - Dev Leader
- YouTube - Dev Leader Path To Tech
- YouTube - Dev Leader Podcast
- YouTube - CodeCommute
- Newsletter - Dev Leader Weekly
- LinkedIn - Nick Cosentino
- GitHub - ncosentino
- Twitter/X - Dev Leader
- Threads - Dev Leader
- Bluesky - Dev Leader
- Mastodon - Dev Leader
- Facebook - Dev Leader
- TikTok - Dev Leader
- Twitch - Dev Leader
- Stack Overflow - Nick Cosentino
Powered by BrandGhost 馃懟