Part 3. Delegates. Group addressing. Creating a chain of methods calling

Part 3. Delegates. Group addressing. Creating a chain of methods calling

This topic is based on the previous topic:


Content


1. What does the term “group addressing of methods” mean?

A delegate can refer to a group of methods that are linked together in a chain. Group addressing is the way (the ability) to create a list of methods that are called automatically when accessing a delegate.

 

2. What are the advantages of using group addressing when working with delegates?

Group addressing allows you to create lists (chains) of calls.

This is effective because:

  • it is possibly flexibly create lists of methods that should be called by one call of the delegate;
  • it is convenient to create lists of methods that perform different types of work on some common object;
  • the structure of the program code improves;
  • it is convenient to handle events that are generated by the system.

 

3. What operations are used for group addressing?

To organize the group addressing of methods using the delegate, 4 operations are used:

  • operations ‘+’ and ‘+ =’ – add a method to the list of methods;
  • operations ‘−’ and ‘−=’ delete the method from the list of methods.

 

 4. Example of adding and removing methods to the list

Let the methods with the names Method1(), Method2(), Method3() be given. Let a delegate with the name List be given.

The following code demonstrates the use of the operations ‘+’, ‘+=’, ‘−’, ‘−=’.

To add Method2 to a List delegate, you need to run this code:

List = List + Method2;

Another way to add a method

ListM += Method2;

To delete the Method2 method, you need to write:

List = List - Method2;

or

ListM -= Method2;

 

5. What happens if try to delete a method that is not in the list of methods?

Nothing will happen. If a method with the name M is not in the list (chain) of methods, then the delete string (the operation ‘−’ or ‘−=’) is skipped.

 

6. How does the group addressing work if the delegate returns a value?

If the method returns a value, then the last method in the list (chain) of methods will be called.

 

7. How does the group addressing work if the delegate returns a void type?

If the delegate returns the type void, then when calling the delegate, all methods that are generated in the list (chain) by group addressing will be called. The sequence of method calls corresponds to the sequence of adding methods to the list. The first method added will be called first, the second method added will be called by the second, and so on.

 

8. An example that demonstrates the use of group addressing for delegates that return a value (methods return a value)

An example in which a delegate returns a value. For the demonstration, methods are chosen that calculate the characteristics of geometric shapes (the circumference, the area of the circle, the volume of a sphere).

Example.

Declarations of the type of the CalcFigures delegate that returns a double value are given:

// Declare a delegate type that returns a value
delegate double CalcFigures(double r);

A description of the static methods that will be formed in the list is given:

// Declaring Static Methods in a Class
// circumference
public static double Get_Lentgh(double r)
{
    double length = 3.1415 * r * 2;
    return length;
}

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

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

In another code (for example, an event handler), delegates are declared and a list (group) of methods referenced by the delegate is formed:

// Declaration of delegates
CalcFigures CF;
CalcFigures GetL = Get_Lentgh;
CalcFigures GetA = Get_Area;
CalcFigures GetV = Get_Volume;

...

// organize group addressing
CF = GetV; // CF is referred to the Get_Length method: CF-> Get

// add to the list CF method Get_Volume
CF = CF + GetV; // or another way CF += GetV;

// add to the list CF method Get_Area
CF += GetA; // CF => Get_Length->Get_Volume->Get_Area
result = CF(3.0); // result = 28.2735 - the Get_Area method is called

// delete the Get_Area method from the CF list
CF -= GetA; // CF => Get_Length->Get_Volume
result = CF(3.0); // result = 113.094 - the Get_Volume method is called

// add the Get_Length method again
CF += GetL; // CF => Get_Length->Get_Volume->Get_Length
result = CF(3.0); // result = 18.849 - the Get_Length method is called

// delete the Get_Length method
CF -= GetL; // CF => Get_Length->Get_Volume
result = CF(3.0); // result = 113.094 - the Get_Volume method is called

// delete the Get_Length method again
CF -= GetL; // CF => Get_Volume - in the list there is one method Get_Volume
result = CF(3.0); // result = 113.094

// delete the last method Get_Volume
CF -= GetV; // CF => list is empty

 

9. An example that demonstrates the use of group addressing for delegates that return a void type

In this example, the group addressing is shown for methods that perform some operation (work) on a real number x, namely:

  • multiplication of the number x by 2 (method Mult2());
  • multiplication of the number x by 3 (method Mult3());
  • the squaring of the number x (Sqr() method).

The delegate type declaration is as follows:

// Delegate type declaration
delegate void CalcNumber(ref double x);

Declaring methods

// methods that handle the real number
// multiplication the number by 2
public static void Mult2(ref double x)
{
    x = x * 2;
}

// multiplication the number by 3
public static void Mult3(ref double x)
{
    x = x * 3;
}

// square of the number x
public static void Sqr(ref double x)
{
    x = x * x;
}

Demonstration of group addressing for a delegate returning a void type from another program code (for example, an event handler):

// Declaring the delegate
CalcNumber CN;

// variable that will be processed by methods
double x;

// creating a chain of methods - 1
CN = Mult2; // CN => Mult2
CN += Mult2; // CN => Mult2 -> Mult2
CN += Sqr; // CN => Mult2 -> Mult2 -> Sqr
CN = CN + Mult3; // CN => Mult2 -> Mult2 -> Sqr -> Mult3

x = 2.0;
CN(ref x); // x = 192 = ((2.0 * 2 * 2)^2 ) * 3

// Deleting all methods from the chain
CN -= Mult3;
CN -= Mult2;
CN -= Mult2;
CN -= Sqr; // CN => list is empty

// creating a chain of methods - 2
CN = Sqr; // CN => Sqr
CN += Sqr; // CN => Sqr -> Sqr
CN += Mult3; // CN => Sqr -> Sqr -> Mult3
CN += Mult2; // CN => Sqr -> Sqr -> Mult3 -> Mult2

x = 2.0;
CN(ref x); // x = 96 = (2.0 ^ 2 ^ 2) * 3 * 2

 


Related topics