C# – delegates in C#

cdelegates

i have 3 files that i need to compare to 100 other files (by using some functions i wrote).
i take each file from the three and in loop compare them to each one of the 100 files.
i want to use the functions sequentially, after i finish to compare the 3 files to the 100 files i want to activate the next function, how can i do it ( the signature of the functions are the same so i can use delegate, but i don't know how)
here is a pseudo code for the comparison:

for(i=0;i<3;i++)
{
 for(j=0;j<100;j++)
   compare(filesArr1[i],filesArr2[j]);
}

After the two loops ended i want to activate the next compare function…

Best Answer

Okay, so basically you're trying to parameterise a common method by the comparison. Something like this should work for you:

public void CompareFiles(IEnumerable<string> firstFiles,
                         IEnumerable<string> secondFiles,
                         Action<string, string> comparison)
{
    foreach (string file1 in firstFiles)
    {
        foreach (string file2 in secondFiles)
        {
            comparison(file1, file2);
        }
    }
}

Then call it with:

CompareFiles(arrFiles1, arrFiles2, FirstComparison);
CompareFiles(arrFiles1, arrFiles2, SecondComparison);

That's assuming the comparison method already takes any appropriate action. If it needs to return something, you'll probably want to use a Func of some description - more details would be useful.