3

I have defined a delegate like this:

   delegate void processRecord(int a, int b);

now i want to use that to create an anonymous method to supply in a function call. The function is defined as such:

   void someFunction(processRecord fcn);

and I want to call someFunction with an anonymous method, but this I cannot get right:

   someFunction(new processRecord(a, b) {
       // do stuff
   });

How is the correct syntax for something like this?

3
  • Do you use processRecord delegate anywhere else? Is not - use Action Commented Jan 10, 2013 at 15:56
  • Yes, it is used at other places. Commented Jan 10, 2013 at 16:12
  • You can at least call it like someFunction((a, b) => /* a and b usage*/); Commented Jan 10, 2013 at 16:29

5 Answers 5

6

The correct syntax to declare the call to method with processRecord parameter is:

someFunction(delegate (int a, int b) {
    // use a and b here
});

You can take advantage of lambda syntax invoking your someFunction, because processRecord is compatible with Action<int,int>, like this:

someFunction((a, x) => Console.WriteLine(a + "; " + x));

But in .net framework there already exist generic delegate Action since .NET 3.5, which you can use instead of creating your own.

So instead you can simply define your function as

void someFunction(Action<int, int> fcn) {}

then call

someFunction( (a,b) => /*use a and b here*/ );
Sign up to request clarification or add additional context in comments.

14 Comments

You don't need to use Action to use a lambda, you can use that lambda, exactly as you have there, with the delegate he's currently using.
@Bart - Action is a predefined delegate type for any void function with any number of parameters up to about 18 or so (not sure on that point). Unless your delegate type uses output or ref parameters, using an Action<> or Func<> like in this example is generally the way to go.
@Servy but you should somehow declare formal parameters, shouldn't you?
The Action<int,int> would allow you to not have to first define the processRecord(int,int) delegate.
Actually, the generic Action<...> delegates were introduced by .Net 3.5.
|
1

You can call it like this:

someFunction(delegate( int a, int b) {
   // do stuff
});

C# compiler will figure out that you are making a delegate processRecord from the context of the call.

1 Comment

Hm. I thought I had tried it. I try it now, and it works. Probably made a mistake in all my fiddling.
1

You can use it if you use deletegate in your someFunction

   someFunction(delegate( int a, int b) {
       // do some stuff
    });

Here is a DEMO.

Comments

0
processRecord t = (q,w) =>
        {
            int n = q+w;
        };
        someFunction(t);

Comments

0

Here are some options, all of which do effectively the same thing:

someFunction((a,b) => DoStuff());

someFunction(delegate(int a, int b){ doStuff(); });

someFunction(new processRecord((int a, int b) => 
{
    doStuff(); doMoreStuff(); 
}));

1 Comment

I'd like to add someFunction((a,b) => { doStuff(); });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.