Using the generic type ‘System.Action<T>’ requires ‘1′ type arguments

The Action and Func delegates are brilliant -- if late -- additions to the .NET framework. A delegate is a method that can be passed as a parameter. They can be used anywhere that you could use an interface with only one method. But delegate syntax is lighter weight than interface syntax.

The type of a delegate is really nothing more than a function signature. But even though the syntax for declaring a delegate is simpler than the syntax for declaring an interface, it is still more complex than it needs to be. Like any other type, a delegate is given a name. But the name is not important, only the signature.

The Action and Func generic delegates were added to the .NET framework in 2.0, which was the first version of the framework to support generics. Using these generics, the signature of a delegate can be declared in line. No separate type needs to be declared, no name needs to be specified, and all information is visible via intellisense.

One problem, however, was that no Action could be specified taking no arguments and returning no value. At least not until .NET 3.5. My guess is that Microsoft left this out so that the compiler wouldn't get confused having a class and a generic with the same name. But .NET 3.5 includes a compiler upgrade (for features like Linq, extension methods, and many more), so they probably fixed this minor issue at the same time. I haven't taken the time to test this theory.

Regardless the reason, if you need an Action with no parameters and no return type, then you have to target .NET 3.5. If you ever get this error message, check your Target Framework setting in the project properties.

2 Responses to “Using the generic type ‘System.Action<T>’ requires ‘1′ type arguments”

  1. Sam Says:

    Thanks man it could have taken ages to find without yout post

  2. Cameron Taggart Says:

    And for .NET 3.5, you must add a reference to the System.Core assembly.

Leave a Reply

You must be logged in to post a comment.