concept anonymous method in category c#

appears as: nonymous methods, n anonymous method, Anonymous methods, onymous method, nonymous method
C# in Depth, Fourth Edition

This is an excerpt from Manning's book C# in Depth, Fourth Edition.

C# 2 introduced method group conversions and anonymous methods. If you wanted to keep the HandleButtonClick method, method group conversions would allow you to change the preceding code to the following:

button.Click += HandleButtonClick;      #1

If your click handler is simple, you might not want to bother with a separate method at all and instead use an anonymous method:

button.Click += delegate { MessageBox.Show("Clicked!"); };     #1

Anonymous methods have the additional benefit of acting as closures: they can use local variables in the context within which they’re created. They’re not used often in modern C# code, however, because C# 3 provided us with lambda expressions, which have almost all the benefits of anonymous methods but shorter syntax:

In chapter 2, you saw how anonymous methods made it much easier to create delegate instances by including their code inline like this:

  • Lambda expressions provide an even simpler way of constructing delegates than anonymous methods. They also allow code to be expressed as data via expression trees, which can be used by LINQ providers to convert C# queries into other forms such as SQL.
  • C# in Depth

    This is an excerpt from Manning's book C# in Depth.

    Behold the lack of the ProductNameComparer type. The statement in bold font creates a delegate instance, which you provide to the Sort method in order to perform the comparisons. You’ll learn more about this feature (anonymous methods) in chapter 5.

    You’ve now fixed all the problems identified in the C# 1 version. That doesn’t mean that C# 3 can’t do better, though. First, you’ll replace the anonymous method with an even more compact way of creating a delegate instance, as shown in the following listing.

    The following listing shows an anonymous method creating an instance of Predicate<T> to return whether the argument passed in is odd or even. Predicates are usually used in filtering and matching—you could use the code in this listing to filter a list for just the even elements, for instance.

    Listing 5.7. Returning a value from an anonymous method
    Predicate<int> isEven = delegate(int x) { return x % 2 == 0; }; Console.WriteLine(isEven(1)); Console.WriteLine(isEven(4));

    The new syntax is almost certainly what you’d have expected—you return the appropriate value as if the anonymous method were a normal method. You may have expected to see a return type declared near the delegate keyword, but there’s no need. The compiler checks that all the possible return values are compatible with the declared return type of the delegate type it’s trying to convert the anonymous method into.

    LINQ in Action

    This is an excerpt from Manning's book LINQ in Action.

    C# 2.0 also added anonymous methods and iterators. These features serve as cornerstones for the new level of integration between data and programming languages.

    We expect readers of this book to know the basics about the features offered by .NET 2.0. We’ll provide you with a refresher on anonymous methods in section 2.4 when we present lambda expressions, and we’ll review iterators in chapter 3.

    More features were required, though, for LINQ to expose query syntaxes natively to languages such as C# and VB.NET. C# 3.0 and VB.NET 9.0 (also known as VB 2008) build on generics, anonymous methods, and iterators as key components of the LINQ facility.

    Delegates existed in C# 1.0, but C# 2.0 was improved to allow working with delegates through anonymous methods. Anonymous methods allow you to write shorter code and avoid the need for explicitly named methods.

    Thanks to anonymous methods, we don’t need to declare a method like Filter. We can directly pass the code to DisplayProcesses, as in listing 2.9.

    Listing 2.9. Calling the DisplayProcesses method using an anonymous method
    DisplayProcesses( delegate (Process process) { return process.WorkingSet64 >= 20*1024*1024; } );

    Instead of using an anonymous method, like in listing 2.9, starting with C# 3.0 we can use a lambda expression.