When a method calls itself
This you can't do in RPG - have a method call itself. Sounds like something you would never use? Think again.
See this small console program that recurses through directories to get a list of folders and files. When the method finds a directory, it calls itself with the sub folder as a parameter whereupon the File part of the IF statement is executed.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Recurse(@"c:\temp");
}
public static void Recurse(string directory)
{
DirectoryInfo path = new DirectoryInfo(directory);
FileSystemInfo[] files = path.GetFileSystemInfos( );
foreach (FileSystemInfo file in files)
{
if (file is DirectoryInfo)
{
Console.WriteLine("Folder-> " + ((DirectoryInfo)file).FullName);
// Now the method calls itself passing in the subfolder name. When the method is called
all the files in the subfolder are listed
Recurse(((DirectoryInfo)file).FullName);
}
if (file is FileInfo)
{
Console.WriteLine("File-> " + ((FileInfo)file).FullName);
}
}
}
}
}
2 Comments:
I don't understand this method calling itself thing. Can you please explain more clearly. Won't it get into an infinite loop?
Flipkart is one of the biggest world wide eCommerce Site flipkart cashback offersso
Flipkart is one of the biggest world wide eCommerce Site Flipkart hdfc offersso
Flipkart is Offering Cashback Offers Flipkart YES Cashback Offer You will get upto 75% Discount
Flipkart is Offering Cashback Offers Flipkart Phonepe Cashback Offer You will get upto 75% Discount
Flipkart is Offering Cashback Offers Flipkart Cashback Offer You will get upto 75% Discount
Post a Comment
Subscribe to Post Comments [Atom]
<< Home