tags:

views:

70

answers:

4

Since Func<> delegates does not take "void" how could i acieve the following in C# 3.0

Func<int, int, void> delg = (a, b) => { Console.WriteLine( a + b); };
+11  A: 

Use Action instead of Func.

    Action<int, int> delg = (a, b) => { Console.WriteLine( a + b); };
Brian
A: 

As an alternative to Action<int, int> you can create your own delegate and return and pass in whatever types you want.

public delegate void MyDelegate(int val1, int val2);

use:

MyDelegate del = (a, b) => {Console.WriteLine( a + b); };
CSharpAtl
A: 
 Func<int, int, Action> foo = (a, b) => () => Console.Out.WriteLine("{0}.{1}", a, b);
  foo(1, 2)();
Florian Doyon
A: 

Func and Action are delegate wrappers, Funcs return a value (the name might come from VBs function..?) and Actions dont.

Like CSharpAtl said, since they are both wrappers, you could just as well make your own delate, with a name that makes more sense to you, (like VoidFunc(int val1 ,int val2)), thought I think Func and Action are pretty standard.

Francisco Noriega