Skip to content

Rich Stokoe

aerospace, cars, finance, leadership, opinion, tech

Menu
  • Learning to Fly
  • Patents
  • Publications
  • Reading List
Menu

Func<T> vs. Action<T>

Posted on June 25, 2012 by rich
Methods…

Although I’ve been using Func<T> and Action<T> for years, I’m embarrassed to say I hadn’t really thought about the differences between them – or more importantly, how they are in fact two sides of the same coin.

Most of the time their use has been a natural reflex action to a coding problem, and the consideration of the other method hasn’t even entered my mind. It’s time I also start using their proper names, which also belies their use:

 

Action<T>

and

Func<TResult>

 

Today’s Problem

I needed a way of enumerating a directory of files and executing a bit of arbitrary code against each one. The code that was to be executed needed the contents of the file. So I instincively wrote something like the following:

 

// Scan files
public void ScanFiles(string path, Func<string, void> codeToExecute)
{
    foreach(string filename in System.IO.Directory.GetFiles(path))
    {
        string fileContents = "";
        using(System.IO.FileStream fileStream = System.IO.File.Open(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read)) 
        {
            using(System.IO.StreamReader streamReader = new System.IO.StreamReader(fileStream))
            {
                fileContents = streamReader.ReadToEnd();
            } // Close stream
        } // Relinquish handle to file

        // Execute the code and pass the contents of the file
        codeToExecute(fileContents);
    }
}

 

But of course, anyone with half a second of experience writing anonymous functions (or just any .NET experience) will know you can’t use “void” as a return type for a generic. I can only blame the fact that it was late in the day, but it took me about 5 minutes to figure out why using

 

Func<string, void>

 

wasn’t allowing me to return void. Of course there is a generic you can use: I should have used Action<string> instead. Action<T> doesn’t require you to define the return type. In fact, you can’t define a return type for Action<T>. (That’s what Func<TResult> is for!

If anybody’s new to this stuff, you can use the following code to give an anonymous function to this method above, which will be given the file’s contents to process:

 

public void ProcessDirectory (string path)
{
    // Call the ScanFiles method, passing in the directory path, and also an anonymous function
    // which shows a message box containing the contents of the file.
    // (Not realistic, but you get the point).
    ScanFiles(path, (contentsOfFile) => {
                   // Do something with the contents of the file
                   MessageBox.Show(contentsOfFile);
    });
}

 

 

Conclusion: The Differences

Func<TResult> lets you pass in an anonymous method which returns the type of TResult. You can also define a Func which takes multiple parameters like this:

 

Func<T1,T2,T3,...,TResult>

 

Meanwhile, Action<T> wont return you anything, but you can define an Action that takes multiple inputs using a similar syntax the the code just above.

 

Action<T1,T2,T3,...>

 

I like anonymous functions. They remind me very much of JavaScript. And JavaScript is the Language of the Gods!*

 

* probably not true…  But it should be!!!!

 

Category: Code

Pages

  • Learning to Fly
  • Patents
  • Publications
  • Reading List

Categories

Architecture (2) Artificial Intelligence (9) Aviation (25) Bad Company (2) Books (1) Cars (2) Civil Aviation (27) Code (17) Companies (28) Entertainment (1) Featured (19) Film (1) Finance (4) Fitness (2) Gaming (4) General Aviation (26) Internet (28) Leadership (1) Military Aviation and Space (1) Mobile (7) Motoring (2) Open Finance (1) Payments (1) People (11) Places and Events (12) Politics (8) Products (6) Quantum Computing (1) Search (6) Security (1) Social (5) Social Business (3) Social Media (6) Software (23) Sports (2) Technology (48) TV (1) Uncategorized (22)

Recent Posts

  • What if your Operating System was an LLM?
  • TOON – The AI Data Format Designed to Save You Money
  • Building a 2nd Brain: Your Personal, AI-Augmented Knowledge Manager
  • State of the AI Nation 5: Walking the Talk
  • Learning to Fly: Lesson 25 (Video)
  • Learning to Fly: Lesson 24
  • Learning to Fly: Lesson 23 (Full Flight Video)
  • State of the AI Nation 4: Open AI Update
  • Learning to Fly: Lesson 22
  • Q-Day and Post-Quantum Cryptography
© 2026 Rich Stokoe | Powered by Minimalist Blog WordPress Theme