C#. Delegates. Part 4. Anonymous functions. Anonymous methods. Returning the value. Passing parameters




Delegates. Part 4. Anonymous functions. Anonymous methods. Returning the value. Passing parameters


Contents


Search other websites:

1. What is called an anonymous function?

An anonymous function is a function that does not have a name but contains only a block of code that it executes. Anonymous functions are convenient to use in conjunction with delegates. Anonymous function can be called only with the help of the delegate. The function itself will not be called directly.

 

2. What kinds of anonymous functions exist in C#?

In the C# programming language, two kinds of anonymous functions are provided:

 

3. In which cases is it expedient to use anonymous functions?

It often happens that a delegate refers only to one method and more to no other. The method itself is not directly invoked. In this case, the method name can be omitted, but only the block of the program code of the method can be declared. The C# programming language provides a declaration of the method code block, which will be passed to the delegate constructor. That is, an unnamed block of program code is declared.

 

4. Advantages of using anonymous functions

Using anonymous functions gives the following advantages:

  • ease of use;
  • the absence of an announcement of a separate method, intended strictly for passing to the delegate.

 

5. What is the general form of an anonymous method declaration, that does not return a value and does not receive parameters?

If the method does not return and does not receive parameters (void), then the general form of this method is:

delegate
{
    // The program code of the method
    // ...
}

 



6. An example of using an anonymous method that does not return a value

Task

Let there be given an application created from a template Windows Forms Application – C#. In this application, the main form corresponds to the class Form1, which has the following form:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TrainDelegates04
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    }
}

Demonstrate a method call that does not receive and does not return a value. The method prints the text of the greeting “Hello world !!!” on the form.

Performing

1. Declare the type of the delegate named HELLO

// A declaration of the delegate type that does not receive and does not return a value
delegate void HELLO();

2. Place a Button control on the form. The result is an object named button1.
3. Program the click event handler on the button1 button. In the event handler, type the following code

// The output of the text 'Hello world !!!' using the delegate
// The delegate is named HW, method - anonymous
HELLO HW = delegate
{
    // the program code of the method
    label1.Text = "Hello world!";
};

// calling an anonymous method using a delegate
HW();

The general program code of the module “Form1.cs”, describing the form class Form1:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TrainDelegates04
{
    public partial class Form1 : Form
    {
        // A declaration of the delegate type that does not receive and does not return a value
        delegate void HELLO();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // The output of the text 'Hello world !!!' using the delegate
            // the delegate is named HW, method - anonymous
            HELLO HW = delegate
            {
                // the program code of the method
                label1.Text = "Hello world!";
            };

            // calling an anonymous method using a delegate
            HW();
        }
    }
}

 

7. How pass an arguments (parameters) to the anonymous method? The general form of an anonymous method that receives a parameter and does not return a value

If an anonymous method needs to pass one or more parameters, then the general form of the declaration of such a delegate is:

delegate (list_of_parameters)
{
    // The program code of the method
    // ...
}

where list_of_parameters – this is one or more parameters that are passed to the delegate. Rules for transferring a list of parameters are the same as for sending parameters to a method (function).

 

8. How to realize a return value from an anonymous method?

Returning the value from the anonymous method is carried out exactly as in the case with the usual method. The return keyword is used to return the value. The general view of an anonymous method that receives input parameters and returns the value is:

delegate (list_of_parameters)
{
    // The program code of the method
    // ...

    return value;
}

where value – variable, expression, or value that is returned from the delegate.

 

9. An example of calling an anonymous method that returns a value and receives parameters

Task

Implement a function that finds the area of the triangle according to Heron’s formula. The function receives the input value of the values of the three sides of the triangle a, b, c. If incorrect values of a, b, c are entered, then the function returns -1 (negative number).

Performing

Declare the type of the delegate in the class (for example, the Form1 form class for applications such as Windows Forms Application).

// Declaring the delegate type
delegate float SquareTriangle(float a, float b, float c);

Implement in a different program code (for example, the event handler) the method of calculating the area of a triangle according to Heron’s formula.

// Declaring a delegate with the name ST, which calculates the area of the triangle
SquareTriangle ST;
ST = delegate(float a, float b, float c)
{
    float s, p, d;

    p = (a + b + c)/2.0f;
    d = p*(p-a)*(p-b)*(p-c);
    if (d<0) return -1.0f;

    s = (float)Math.Sqrt(p*(p-a)*(p-b)*(p-c));
    return (float)s;
};

// call a delegate
float area;
area = ST(2.0f, 3.0f, 2.2f); // area = 2.199636
area = ST(3.0f, 4.0f, 5.0f); // area = 6.0
area = ST(1.0f, 5.0f, 7.0f); // area = -1.0

This example is described in more detail in the article:

 

10. What are the features of applying external variables in anonymous methods?

In anonymous methods, you can use external variables. External variables for anonymous methods are the local variables of the method from which the anonymous method is called.

An example

In some class, let declared the external variable a and the delegate type CalcA:

// external variable a
private int a;

// the type CalcA of delegate
delegate void CalcA();

A code snippet that demonstrates the use of the variable a in an event handler or another method of the same class:

// Declaring an anonymous method without parameters
CalcA IncrA = delegate
{
    // "capture" of an external variable a
    a = a + 1; // increment
};

// Demonstration of the use ("capture") of an external variable a
a = 8;

IncrA(); // a = 9
IncrA(); // a = 10

 

11. What does the term “variable capture” mean? Example

The term “variable capture” means the use of an external variable in an anonymous method. The captured variable exists until the delegate who captured it, will not be “garbage collected”.
More details about “garbage collection” are described in the topic:

Example

The example demonstrates the “capture” of the variable a. An example is given for a Windows Forms Application. In the main form class, two types of delegates are declared with the names CalcA1 and CalcA2.

Two delegates are declared in the event handler:

  • delegate IncrA of type CalcA1. This delegate increases by 1 the value of the external variable a;
  • delegate IncrA2 of type CalcA2. This delegate increases the value of the external variable a by 2.

The delegates IncrA and IncrA2 carry out the “capture” of the external variable a.

The entire text of the module “Form1.cs” is as follows:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TrainDelegates07
{
    public partial class Form1 : Form
    {
        // external variable a
        private int a;

        // the type CalcA1 of delegate
        delegate void CalcA1();

        // the type CalcA2 of delegate
        delegate void CalcA2();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Declaring an anonymous method
            CalcA1 IncrA = delegate
            {
                // "capture" of an external variable a
                a = a + 1; // increasing a by 1
            };

            // the anonymous method Incr2
            CalcA2 IncrA2 = delegate
            {
                // "capture" of an external variable a
                a = a + 2; // increasing a by 2
            };

            // Demonstration of the "capture" of an external variable a
            a = 8;

            IncrA(); // a = 9
            IncrA(); // a = 10

            IncrA2(); // a = 12, the previous value of a is taken
        }
    }
}

 


Related topics