Deep C# - Anonymous Methods, Lambdas And Closures
Deep C# - Anonymous Methods, Lambdas And Closures
Monday, 19 September 2016
Article Index
Deep C# - Anonymous Methods, Lambdas And Closures
From Anonymous Methods to Lamdas
Expressions Lambda to Expression Trees
Closure
Two Patterns for Closure

Anonymous methods aren't particularly new, but they have hidden depths and lead on to lambdas and the idea of a closure. These are all important ideas in modern programming.

This is a chapter of our ebook on C#, a work in progress.

Deep C#

 coverCsharp

 Chapter List

  1. Value And Reference
    Value and reference are a fundamental division in the way C# treats data. It is important that you understand the differences and most importantly when to use a struct and when to use a class. These aren't just differences in efficiency, they affect the semantics too.

  2. Passing Parameters
    Passing parameters is easy as it always works in the same way but the effects aren't always the same. It can be confusing and even error prone unless you understand how it all works.

  3. Inheritance
    Inheritance is a simple idea, until you try to make use of it.

  4. Casting – the escape from strong typing
    Casting is one of the most confusing aspects of any modern language and it often makes beginners think hard. But if you know why you are doing it then the how makes a lot more sense.

  5. Controlling Inheritance
    Inheritance is a great idea but it is a powerful technique that can be misused. C# provides the tools to keep inheritance under control or to turn it off completely if you want to.

  6. Delegates
    Delegates are C#'s original way of allowing you to work with functions as if they were first class objects. The aim may be simple but the need to define a type and then an instance of the type can be confusing. Let's see if we can make it all seem logical.

  7. Multicast delegates and events
    Multicast delegates are useful in their own right but they also form the basis on which the C# event system is built. We take a close look at how they work and how to use them. For example, did you know you could add and subtract delegates?

  8. Anonymous Methods, Lambdas And Closures
    Anonymous methods aren't particularly new, but they have hidden depths and lead on to lambdas and the idea of a closure. These are all important ideas in modern programming.

  9. Take Exception To Everything

  10. Dangerous Pointers
    In C# pointers have been replaced by references but there is still a need for the raw dangerous original concept. You could say "what's so dangerous about pointers?"
Multicast delegates and events
Tuesday, 25 May 2010
Article Index
Multicast delegates and events
Events
Generic Events

Multicast delegates are useful in their own right but they also form the basis on which the C# event system is built. We take a close look at how they work and how to use them. For example, did you know you could add and subtract delegates?

 

 

Anonymous methods were introduced back in .NET 2.0 and while they sound like something designed to implement “dirty” shortcuts they are a welcome addition to C#. In modern C# they have been superseded by the lambda expressions but there is still one thing that an anonymous method can do that a lambda can't so they are still worth knowing about. In addition statement lambdas are just a slightly different syntax for anonymous methods so they are the foundations upon which lambdas are built.

The big problem with anonymous methods is figuring out what the problem is that they are designed to solve.

So let’s take a look at what they are for.

On one level anonymous methods are just about making delegates easier to create. Let's go through the stages of creating a delegate.

Delegates

A delegate is just an object that “wraps” a function.

It is what you need when functions are not in themselves first class objects. That is you can't pass a function as argument to another function so you have to wrap it in an object so that you can pass the object and a delegate is just an object designed for wrapping a function.

Before you can use a delegate to wrap a function you have to create a delegate type that has the signature of the function you want to wrap. That is every delegate wrapper carries information about the function it wraps in its type - this is used to detect simple errors. 

Then you instantiate the type, wrap the function and use it.

So the steps to create and use a delegate are:

  1. Create the delegate type which specifies the signature and return type.
  2. Write the method that you want the delegate to "wrap".
  3. Instantiate the delegate type created in step 1 and use it to wrap the method.

What all this means is that you have to invent multiple names for what in most cases is a single idea -

  • the delegate type - MyDelegateType
  • the method to be wrapped - MyMethod

and

  • the instance doing the wrapping - MyDelegate

For example, if you want to wrap a “Hello World” function you first create a suitable delegate type:

delegate void MyHelloDelegateType();

then you have to create the function within some class or other:

void Hello(){
 MessageBox.Show("Hello From a Delegate");
}

and finally create an instance of the delegate type specifying the function that it is to wrap:

MyHelloDelegateType MyHelloDelegate1 =
             new MyHelloDelegateType (Hello);

or equivalently:

MyHelloDelegateType MyHelloDelegate1 = Hello;

Calling the function via the delegate is just a matter of using its name:

MyHelloDelegate1();

You can see that we have had to invent three names:

  • MyHelloDelegateType - the delegate type
  • Hello - the method name
  • MyHelloDelegate - the wrapper instance

This is fine if you are going to create multiple instances of the type and wrap multiple functions but in most cases the type, the delegate and the method are more or less a single entity.

That is you create a delegate because you want to use a method as if it was an object and often this is a one-off requirement.

csharp

 

 



Last Updated ( Thursday, 22 September 2016 )
 
 

   
Banner
RSS feed of all content
I Programmer - full contents
Copyright © 2017 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.