C#. The concept of a delegate. Using a delegate in the program




Part 1. The concept of a delegate. The declaration of delegate type. Using a delegate in the program. Group transformation of methods


Contents


Search other websites:

1. What are the delegates used for?

A delegate is an object that can refer to a method. In fact, the delegate encapsulates a method. After creating a delegate, you get an object that contains a reference to the method. Using the delegate, you can invoke the method that this delegate refers to.

C# delegate assignment figureFigure 1. Delegate assignment

 

2. What is the general form of a delegate type declaration? Why use the keyword ‘delegate’?

To get a delegate, you first need to declare its type. The delegate type is declared using the keyword ‘delegate’. The following is the general form of the delegate declaration:

delegate returned_type name(list_of_parameters);

where

  • returned_type – indicates the type of the value that is returned by methods that will be called by the delegate;
  • name – the name of delegate type. With this name, delegates are declared in the same way as ordinary variables are declared;
  • list_of_parameters – the parameters that are required for methods that will be called by the delegate.

 

3. What is the general form of the delegate’s declaration?

After the declaration of the delegate type, you can declare the delegate himself. The general form of declaring a delegate (object) is exactly the same as in the case of declaring a variable of some type:

delegate_type name1 [, name2, ... ];

where

  • delegate_type – the name of the delegate type that is declared with the delegate keyword (see p. 2);
  • name1 , name2, … – the names of delegates. Using these names, you can access the methods referenced by delegates.


 

4. Is it possible to use a delegate to call a different methods?

Yes. The delegate can invoke a different methods. If the delegate refers to Method1, then to call the Method2 using the delegate, you need to change the reference to this method in this delegate (Method2).

C# delegate reference method figure

Figure 2. The delegate refers to Method2

 

5. The simplest example of a delegate declaration and its use in a program

This example is for applications that are created using the Windows Forms Application template.

An example of creating an application of the Windows Forms Application type is described in detail in the topic:

The practical implementation of this example is described in detail in the article:

 

5.1. Task

In an application of the Windows Forms Application type, declare the type of the delegate that refers to the method. The requirements for the method signature are as follows:

  • the method gets a variable of type double as a parameter;
  • method returns a double value, which is the result of the calculation.

Implement the method calls using a delegate that gets the radius R and compute:

  • the circumference by formula D = 2 · π · R;
  • area of the circle by the formula S = π · R2;
  • the volume of a sphere: V = 4/3 · π · R3.

Methods must be declared as static (using the static keyword).

 

5.2. Reflections

For work of the program we choose such names:

  • the delegate type name is CalcFigure;
  • delegate name (object instance) – CF;
  • the name of the method that calculates the circumference – Get_Length ();
  • the name of the method that computes the area of a circle – Get_Area();
  • the name of the method that computes the volume of the sphere – Get_Volume().

The delegate type and method type declaration is implemented in the Form1 class of the main application form, such as Windows Forms Application.

 

5.3. Solving (code snippets)
5.3.1. Declaring the type of delegate

The delegate type is declared in a certain class. It can be, for example, the main form class in case an application is created using the Windows Forms Application template.

In the class, you need to declare the type of the delegate named CalcFigure in accordance with the task. A fragment of such a declaration is as follows:

// declaring a CalcFigure Delegate Type
delegate double CalcFigure(double r);

 

5.3.2. Declaring methods in a class

Methods in the class are declared as static with the ‘static’ keyword.

// declaring a static methods in a class
// the circumference
public static double Get_Length(double r)
{
    double length;
    length = 2 * 3.1415 * r;
    return length;
}

// area of a circle
public static double Get_Area(double r)
{
    double area;
    area = 3.1415 * r * r;
    return area;
}

// volume of a sphere
public static double Get_Volume(double r)
{
    double volume;
    volume = 3.1415 * r * r * r * 4.0 / 3.0;
    return volume;
}

 

5.3.3. Demonstration a calling of methods using delegate

Demonstration a calling of methods using delegate from other program code, for example, from the button click event handler (Windows Forms Application template).

// event handler button1_Click()
private void button1_Click(object sender, EventArgs e)
{
    double radius, length, area, volume;
    radius = Convert.ToDouble(textBox1.Text);

    // calling a delegate
    // construct a delegate
    CalcFigure CF = new CalcFigure(Get_Length);
    length = CF(radius); // 1. Calling the Get_Length() method

    CF = new CalcFigure(Get_Area);
    area = CF(radius); // 2. Calling the Get_Area() method

    CF = new CalcFigure(Get_Volume);
    volume = CF(radius); // 3. Calling the Get_Volume() method

    label2.Text = length.ToString();
    label3.Text = area.ToString();
    label4.Text = volume.ToString();
}

Figure 3 schematically shows the work of delegate CF.

C# delegate reference method figure

Figure. 3. Delegate CF

 

6. How is group transformation of delegated methods carried out? Example

Group conversion of delegated methods allows you to simplify the assignment string to the delegate method. In this case, the keyword new is omitted.

In the above example (see section 5), the line

CF = new CalcFigure(Get_Length);

can be replaced by a line

CF = Get_Length;

which simplifies the program code and the visibility of its display.

Considering the above, the corresponding event handler button1_Click() can have the following appearance:

// group conversion of delegates
private void button1_Click(object sender, EventArgs e)
{
    double radius, length, area, volume;
    radius = Convert.ToDouble(textBox1.Text);

    // replacement of operator 'new' by assignment operator
    // construct a delegate named CF
    CalcFigure CF = Get_Length; // do not need the 'new' operator - simplify the code
    length = CF(radius); // calling the method Get_Length()

    CF = Get_Area; // The CF delegate refers to Get_Area
    area = CF(radius); // calling the method Get_Area()

    CF = Get_Volume; // the CF delegate refers to Get_Volume
    volume = CF(radius); // Calling the method Get_Volume()

    label2.Text = length.ToString();
    label3.Text = area.ToString();
    label4.Text = volume.ToString();
}

 


Related topic