C#, Generic, Reuse of file enumeration

Posted on 2009-05-13
Last Modified: 2013-12-17
I'm trying to use my FileWorks class below to tell me if a directory has a certain file extension.
I'm trying to be as generic as possible and reuse the function...

I'm not sure how to approach this one. This is the code I have below.

Does not look like it would work if I were to call it...

FileWorks filework = new FileWorks();
filework.GetFilesDoSomething(directory, CheckDirectoryForFilesWithExtension);

How can I pass the bool result back accross soo many functions?

public class FileWorks
        bool CheckDirectoryForFilesWithExtension(FileInfo fi)
            if (fi.Extension == "tif")
                return true;
        public void GetFilesDoSomething(string DirName, Action<FileInfo> method)
            DirectoryInfo d = new DirectoryInfo(DirName);
            FileInfo[] f = d.GetFiles();
            foreach (FileInfo fi in f)

Open in new window

Question by:silentthread2k
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 16

Expert Comment

ID: 24377680
This looks like delegates 101, not generics.

Check this out:

LVL 33

Accepted Solution

Todd Gerbert earned 500 total points
ID: 24378736
This codes isn't really "proper", but it does demonstrate using a delegate to make your GetFilesDoSomething() re-usable.
class Program
	static void Main(string[] args)
		FileWorks.GetFilesDoSomething("C:\\", new FileWorks.SomethingDelegate(FileWorks.CheckDirectoryForFilesWithExtension));
public static class FileWorks
	public delegate void SomethingDelegate(object inParam, out object returnVal);
	public static void CheckDirectoryForFilesWithExtension(object fi, out object returnValue)
		if (((FileInfo)fi).Extension.ToLower() == ".tif")
			returnValue = true;
			returnValue = false;
	public static void GetFilesDoSomething(string dirName, SomethingDelegate method)
		DirectoryInfo d = new DirectoryInfo(dirName);
		FileInfo[] f = d.GetFiles();
		foreach (FileInfo fi in f)
			object returnValue;
			method(fi, out returnValue);
			Console.WriteLine("{0} is a TIF: {1}", fi.Name, (bool)returnValue);

Open in new window


Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Data Analysis 7 59
async questions 5 49
C#:  Need to switch between timer loop and token driven loop 2 32
Replace statements HTML with HTML IF 8 67
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below.…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question