Click here to Skip to main content
15,888,031 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
HI ALL codeproject members,
What does it mean?

Using the generic type ‘System.Action'<t>requires 1 type arguments</t>

Thank you
Posted
Updated 10-May-11 8:34am
v2

First off, there is a lot of info out there: a simple google could have got you relevenat info very quickly: MSDN[^]

You use an Action<T> delegate to pass a method as a parameter to another method without having to declare a formal delegate. The method you pass can return no value, and takes a single parameter of type "T".

This is taken direct from MSDN:
delegate void DisplayMessage(string message);
   public static void Main()
   {
      DisplayMessage messageTarget; 
      if (Environment.GetCommandLineArgs().Length > 1)
         messageTarget = ShowWindowsMessage;
      else
         messageTarget = Console.WriteLine;
      messageTarget("Hello, World!");   
   }      
   private static void ShowWindowsMessage(string message)
   {
      MessageBox.Show(message);      
   }

Can be simplified to:
   public static void Main()
   {
      Action<string> messageTarget; 
      if (Environment.GetCommandLineArgs().Length > 1)
         messageTarget = ShowWindowsMessage;
      else
         messageTarget = Console.WriteLine;
      messageTarget("Hello, World!");   
   }      
   private static void ShowWindowsMessage(string message)
   {
      MessageBox.Show(message);      
   }
</string>
Both examples do the same job: you can specify that output goes to a message box (via ShowWindowsMessage) or to the console once in advance, and never have to check again.
The second example just saves you having to declare a delegate to define what method signatures can be used.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 10-May-11 15:45pm    
Good explanation, my 5. I thought the illustrating code could be much shorter, but this one is still good.
--SA
OriginalGriff 10-May-11 15:48pm    
Hi SA! Shorter would be nice, I agree, but you can't cut out much without losing the sense of why you are using a delegate in the first place! Or at least, I can't. :laugh:
Sergey Alexandrovich Kryukov 10-May-11 15:49pm    
I decided it add my answer to bring OP's attention to generics in general, please see.
--SA
The compiler error tells you that exactly. You are using System.Action<t> that expects a type argument and you are not passing that. Refer the link below:

http://msdn.microsoft.com/en-us/library/018hxwa8.aspx[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 10-May-11 15:46pm    
That should explain everything provided OP takes it seriously. My 5.
I feel that OP needs to learn about generics in general, first and foremost.
--SA
Sergey Alexandrovich Kryukov 10-May-11 15:49pm    
So, I added my answer for this purpose, please see.
--SA
I think you need to learn about generics first. This is really important. After you understand it, your problem won't be a problem for you.

Start here:
http://msdn.microsoft.com/en-us/library/ms172192.aspx[^].

—SA
 
Share this answer
 
Comments
Karthik. A 10-May-11 16:23pm    
Spot on, you are right!
Sergey Alexandrovich Kryukov 10-May-11 17:36pm    
Thank you.
--SA
Google:
http://adventuresinsoftware.com/blog/?p=281[^]

Looks like you need to target .NET 3.5.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 10-May-11 15:43pm    
Not exactly. When Action and Func were not available in in 2.0, I declared them myself and they worked exactly the same way. There is absolutely no "compatibility" barrier. The classes expecting Action and Func ***do not*** require these declaration. It's just few lines of delegate declarations to write.

Thought it's good to know.
--SA
Thank you for all your help.
see this picute [^]to precis exactely the problem ,,,
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900