?? A Lambda Equivalent of this Delegate ??

T

Tom Baxter

Hi all,

Let's say I have this delegate:

delegate void MyDelegate<T1, T2>(T1 x, T2 y);

I can easily make an anonymous method of the above delegate type:

MyDelegate<int, float> md1 = delegate(int x, float y) {
Console.WriteLine("In the md);
};


If I do not need to reference the two parameters I can do this:

MyDelegate<int, float> md = delegate {
Console.WriteLine("In the md()");
};


My question is, can I create a lambda that will be equivalent to the
parameter-less anonymous method shown above? If I have no need to reference
the parameters, I don't want to. I tried the following two approaches,
neither or which work:

MyDelegate<int, float> md2 = () => {
Console.WriteLine("In the md()");
};

MyDelegate<int, float> md2 = => {
Console.WriteLine("In the md()");
};


Thanks very much.
 
J

Jeroen Mostert

Tom said:
Let's say I have this delegate:

delegate void MyDelegate<T1, T2>(T1 x, T2 y);

I can easily make an anonymous method of the above delegate type:

MyDelegate<int, float> md1 = delegate(int x, float y) {
Console.WriteLine("In the md);
};


If I do not need to reference the two parameters I can do this:

MyDelegate<int, float> md = delegate {
Console.WriteLine("In the md()");
};


My question is, can I create a lambda that will be equivalent to the
parameter-less anonymous method shown above? If I have no need to
reference the parameters, I don't want to.

Too bad, you'll have to. The anonymous method syntax above is the only one
that allows you to omit the parameters.

Or, you know, just don't use a lambda. It's just another way of writing
anonymous methods; there is no benefit to using one here. Lambdas only
really become useful when you want to wrap a simple expression as a
delegate. A lambda for a delegate that has no return value goes slightly
against the spirit (but is still valid).

Note also that declaring custom delegate types is now slightly discouraged
in favor of using the Action<> and Func<> types, as custom delegate types
clutter up namespaces and using generic types allows for interfacing with
LINQ without converting delegates. Your MyDelegate<> above would be an
Action<> instead. Sometimes you may want to leverage the type system to make
sure people don't mix wildly different delegates that happen to have the
same types, but usually there's no point (or, as in the case of LINQ, you
rather want to do the exact opposite and make sure delegates with equal
parameter and return value types are interchangeable).
 
P

Peter Duniho

[...]
If I do not need to reference the two parameters I can do this:

MyDelegate<int, float> md = delegate {
Console.WriteLine("In the md()");
};

My question is, can I create a lambda that will be equivalent to the
parameter-less anonymous method shown above?

Yes and no. With a lambda expression, you'll have to specify the
arguments. But you don't have to provide types for them (the compiler
will infer the types) and of course you don't have to use them. So the
declaration can be brief:

MyDelegate<int, float> md2 = (x, y) => Console.WriteLine("In the
md()");

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top