3

Recently, i read "Professional C# 5.0 and .NET 4.5.1 by Christian Nagel; Jay Glynn; Morgan Skinner" book. And i confused about: "in case,the syntax of anonymous methods simpler than syntax of lambda expressions" in book.

Details in Chapter 8: Delegates, Lambdas, and Events

LAMBDA EXPRESSIONS "NOTE The syntax of lambda expressions is simpler than the syntax of anonymous methods. In a case where a method to be invoked has parameters and you don’t need the parameters, the syntax of anonymous methods is simpler, as you don’t need to supply parameters in that case."

Can anyone explain/sample why anonymous methods don't need to supply parameters in that case?

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
zizifn
  • 395
  • 1
  • 2
  • 14
  • *"There is one case in which an anonymous method provides functionality not found in lambda expressions. Anonymous methods enable you to omit the parameter list. This means that an anonymous method can be converted to delegates with a variety of signatures. This is not possible with lambda expressions."* https://msdn.microsoft.com/en-us/library/0yw3tz5k.aspx – LukeH Feb 03 '15 at 12:53

3 Answers3

5

Because this is valid

Func<int,int> f = delegate { return 47; }

But this is not

Func<int,int> f = () =>  47;

With anonymous method syntax you can omit the parameters if you don't need them. But in lambda expression you have to supply parameters.

This has been also stated in the documentation:

There is one case in which an anonymous method provides functionality not found in lambda expressions. Anonymous methods enable you to omit the parameter list.This means that an anonymous method can be converted to delegates with a variety of signatures.

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
  • 2
    An oft-used convention is to replace the unused lambda parameter with an underscore, in which case your second example would become `Func f = _ => 47;`, which is valid, straightforward and readable (so long as everybody understands and accepts the underscore convention). – LukeH Feb 03 '15 at 12:37
3

Can anyone explain/sample why anonymous methods don't need to supply parameters in that case?

Because if you're not using the delegate parameters, you can leave it up to the compiler to auto-generate them for you.

Example:

internal delegate void MyDelegate(string s);

public class Foo
{
    public void F()
    {
        MyDelegate del = delegate { Console.WriteLine("hello!"); };
    }
}

When i specify no parameters (because im not explicitly using them in my delegate), the compiler translates it into the following:

public void F()
{
    MyDelegate del = delegate(string param0)
    {
        Console.WriteLine("hello!");
    };
}

Or if you want the real nasty stuff:

[CompilerGenerated]
private static void <F>b__0(string param0)
{
    Console.WriteLine("hello!");
}

public void F()
{
    if (Foo.CS$<>9__CachedAnonymousMethodDelegate1 == null)
    {
        Foo.CS$<>9__CachedAnonymousMethodDelegate1 = new MyDelegate(Foo.<F>b__0);
    }
    MyDelegate del = Foo.CS$<>9__CachedAnonymousMethodDelegate1;
}
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
0

If your method expected a parameter you need to define/use it eitherway be it lambda expressions or anonymous methods.
Lets say you have a delegate declared like below.

public delegate void PrintDelegate(string message);

you can assign either lambda expression or anonymous method to it like below

Lambda Expression

PrintDelegate p1 = message => { Console.WriteLine(message); };

Anonymous Expression

PrintDelegate p2 = delegate(string text) { Console.WriteLine(text); };

In both these you see that the parameter has be defined.
However if you want to hook the delegate to another method with the same signature you can do like below.

PrintDelegate p1 = Console.WriteLine;

Console.WriteLine has the same signature as the defined delegate so you need not specify the parameter explicitly when defining the delegate.

All these variations can be invoked normally and will give the same output.

p1("Hello World");

Note other answers where the parameters is not used in defining the anonymous method you cannot use the passed value of the parameter inside the anonymous method which is not the ideal way to declare a delegate body.

Vignesh.N
  • 2,618
  • 2
  • 25
  • 33