user160677
user160677

Reputation: 4303

How can I create a C# Delegates that does not return?

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); };

Upvotes: 1

Views: 173

Answers (4)

Francisco Noriega
Francisco Noriega

Reputation: 14604

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.

Upvotes: 0

Florian Doyon
Florian Doyon

Reputation: 4186

    Func<int, int, Action> foo = (a, b) => () => Console.Out.WriteLine("{0}.{1}", a, b);
        foo(1, 2)();

Upvotes: 0

CSharpAtl
CSharpAtl

Reputation: 7522

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); };

Upvotes: 0

Brian
Brian

Reputation: 118895

Use Action instead of Func.

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

Upvotes: 11

Related Questions