Part 6 Real time example of recursion

Link for code samples used in the demo

Link for csharp,,, dotnet basics, mvc and sql server video tutorial playlists

This is continuation to Part 5. Please watch Part 5 from C# Interview Questions tutorial before proceeding.

Probably in an interview, an interviewer may ask you to give an example, where you have used recursion in your project.
To find all the files in a folder and in all the sub-folders in the hierarchy. This is a very good example of where we could use recursion.

// find all files in subdirectories c#.png

Please make sure to include the following using declaration.
using System.IO;

public class Program
public static void Main()
// Prompt the user to enter folder path
Console.WriteLine(“Please enter folder path”);
// Read the path from the console
string folderPath = Console.ReadLine();

// Invoke FindFiles() recursive function

private static void FindFiles(string path)
// Loop thru each file in the current directory and print it’s name
foreach (string fileName in Directory.GetFiles(path))

// If there is a subdirectory in the current directory, then recursively call FindFiles() method.
// The recursion will break when the innermost directory with no subdirectory is found.
foreach (string directory in Directory.GetDirectories(path))
// Notice that FindFiles() is calling itself

    .Net abstract abstract class advantage Advantages array arraylist AS benefits block C (Programming Language) C# c# class default constructor access modifier c# interview questions and answers c# reflection constructor access modifier c# reflection constructor not found c# reflection default constructor c# reflection get constructor c# reflection private constructor c# reflection protected constructor call cast catch class constructor csharp Data DataTypes difference between Difference between int and Int32 in c# different directory dot net dotnet Each example exception exceptions factorial files Finally folder frequently asked function generic Handle happens int vs int32 interfaces interview is jagged keyword LINQ list Lists method Number occur Operator Overriding purpose question questions Real Real-time real-world realtime Recursion recursive reverse Sentence single store String Structure subdirectories Throw throws time try Types Use using virtual method what when where Why Word World