C#. Delegates. Part 5. Examples of passing arrays, structures, classes to an anonymous method




Delegates. Part 5. Examples of passing arrays, structures, classes to an anonymous method


Contents


Search other websites:

1. An example of calling an anonymous method that gets an array of integers as input parameter

Declaring a delegate type named ProcessArray that receives an array of numbers as input and does not return a value

// declaration of the type of the delegate that receives the array of numbers as a parameter
delegate void ProcessArray(ref int[] A);

Demonstration of delegates like ProcessArray with two anonymous methods. The first method zeroes all items of the array. The second method increments the value of array items by 10.
The source code that demonstrates the work of anonymous methods:

// Passing arguments to an anonymous method
// A ProcessArray delegate is declared
// Delegate PA1 zeroes the array
ProcessArray PA1 = delegate (ref int[] A)
{
    // zeroing an array of integers
    int i;
    for (i = 0; i < A.Length; i++)
        A[i] = 0;
};

// Delegate PA2 increases the value of array elements by 10
ProcessArray PA2 = delegate(ref int[] A)
{
    int i;
    for (i = 0; i < A.Length; i++)
        A[i] = A[i] + 10;
};

// Demonstration of the work of delegates
// Declaring an array of 5 integers
int[] B = new int[5];

// calling the delegate PA1 - zeroing the array
PA1(ref B); // B = [0, 0, 0, 0, 0]

// calling the delegate PA2 - increasing the array elements by 10
PA2(ref B); // B = [10, 10, 10, 10, 10]

 

2. An example of calling an anonymous method that receives a structure as input parameter

Let the class description have a structure of the type Student:

// Declaring a structure of type Student
struct Student
{
    public string Name;
    public string Address;
    public int Age;
};

To demonstrate the use of an anonymous method in the body of the structure, you must first declare the type of the delegate. The delegate type is declared in the same class as the structure type. The delegate declaration, for example, may be as follows:

// Declaring the delegate type
delegate void ShowInfo(Student st);

Below is the code that demonstrates how to use the delegate type ShowInfo:

// Demonstrating the use of an anonymous method that receives a structure as a parameter
// 1. Declaring an anonymous method
ShowInfo SI;
SI = delegate(Student st)
{
    label1.Text = st.Name;
    label2.Text = st.Address;
    label3.Text = st.Age.ToString();
};

// 2. Declaring and filling the Stud variable
Student Stud;
Stud.Name = "Ivanov I.I.";
Stud.Address = "The address of Ivanov I.I.";
Stud.Age = 118;

// 3. Calling an anonymous method using a delegate
SI(Stud);

 

3. An example of an anonymous method that receives as input parameter an array of structures

In the example, an array of structures of type Student is passed to an anonymous method.
The structure of the type Student is declared in a certain class and has the form:

// Declaring a structure of type Student
struct Student
{
    public string Name;
    public string Address;
    public int Age;

    public void Set(string name, string address, int age)
    {
        Name = name;
        Address = address;
        Age = age;
    }
};

The delegate type declaration is described in the same class as the Student structure.

// Declaring the delegate type
delegate void ShowInfoIndex(Student[] st, int index);

Demonstration of passing of an array of structures to an anonymous method

// Passing an array of structures to a method
// 1. Declaring a variable st of type "array of structures"
Student[] st = new Student[3];

// 2. Filling an array of structures st
st[0].Set("Ivanov I.I.", "Minsk", 32);
st[1].Set("Johnson J.J.", "Kiev", 45);
st[2].Set("Kameroon J.", "Las-Vegas", 23);

// 3. Declaring a delegate variable with the SI name and the code of the anonymous method
ShowInfoIndex SI;
SI = delegate(Student[] ST, int index)
{
    label1.Text = ST[index].Name;
    label2.Text = ST[index].Address;
    label3.Text = ST[index].Age.ToString();
};

// 4. Calling an anonymous method using a delegate
SI(st, 0); // Will be displayed: {"Ivanov I.I.", "Minsk", 32}
SI(st, 1); // Will be displayed: {"Johnson J.J.", "Kiev", 45}

 

4. An example of an anonymous method that receives an object of an instance of the class

Let the description of the class that is placed in a separate module (for example “MyClass1.cs”) be given. The class code is the following

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TrainDelegates07
{
    // Declaring a class in the module "MyClass1.cs"
    class MyClass1
    {
        private int x;
        private double R;

        public int GetX()
        {
            return x;
        }

        public double GetR()
        {
            return R;
        }

        public void SetX(int nx)
        {
            x = nx;
        }

        public void SetR(double nR)
        {
            R = nR;
        }
    }
}

The class implements two internal variables (x, R) and four methods that read and change the values of these internal variables. In another class (module), the delegate type is declared as follows:

// declaration of the delegate type that gets the class MyClass1
delegate void ProcessMyClass(MyClass1 mc);

In a method that demonstrates the operation of anonymous methods (for example, an event handler), you need to execute approximately the following code

// Demonstration of the use of the class
// 1. Declare a class object
MyClass1 MC1 = new MyClass1(); // allocate memory for MC1

// 2. Fill class fields with values
MC1.SetX(5);
MC1.SetR(10.5);

// 3. Declare a delegate of type ProcessMyClass
ProcessMyClass PC;
// The delegate refers to an anonymous method that gets the class
PC = delegate (MyClass1 mc)
{
    int x;
    double R;

    // output on the form the values x, R
    x = mc.GetX();
    R = mc.GetR();

    label1.Text = x.ToString();
    label2.Text = R.ToString();
};

// 4. Calling an anonymous method using a delegate
PC(MC1);

 

5. An example of an anonymous method that gets an array of object instances of the class

The task

It is given a class that is described in a separate module “MyClass1.cs”. The code of module is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TrainDelegates08
{
    // class declaration MyClass1
    class MyClass1
    {
        private int x;
        private double R;

        public int GetX()
        {
            return x;
        }

        public double GetR()
        {
            return R;
        }

        public void SetX(int nx)
        {
            x = nx;
        }

        public void SetR(double nR)
        {
            R = nR;
        }
    }
}

It is needed to declare an anonymous method that takes an array of classes of type MyClass1 as the input parameter. It is needed to realize the demonstration of the use of an anonymous method in the Form1 class.



Solution

Program code of the Form1.cs module 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 TrainDelegates08
{
    public partial class Form1 : Form
    {
        // declaration of the delegate type, which receives an array of classes of the type MyClass1
        delegate void ProcessMyClass(MyClass1[] mc, int index);

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Demonstration of passing an array of classes to an anonymous method
            // 1. Declare an object named MC of the type "array of 10 classes"
            MyClass1[] MC;

            // 2. Memory allocation
            // 2.1. Allocate memory for an array of classes as a whole
            MC = new MyClass1[10];

            // 2.2. Allocate the memory for each element of the array (for the class)
            for (int i = 0; i < 10; i++)
                MC[i] = new MyClass1();

            // 3. Fill the fields of a class array with values
            for (int i=0; i<10; i++)
            {
                MC[i].SetX(i + 1);
                MC[i].SetR(10 * i + 1);
            }

            // 4. Declare the delegate of ProcessMyClass type
            ProcessMyClass PC;

            // 5. Describe the program code of an anonymous method that receives an array of classes
            PC = delegate (MyClass1[] mc, int index)
            {
                // displaying on the form of a class instance values with the number 'index'
                int x;
                double R;
                x = mc[index].GetX();
                R = mc[index].GetR();
                label1.Text = x.ToString();
                label2.Text = R.ToString();
            };

            // 6. Calling an anonymous method using the PC delegate
            PC(MC, 5);
        }
    }
}

Let’s explain some fragments of the code.

In the Form1 class, the delegate type is declared, which receives a class array as an input parameter. The code snippet is as follows:

// declaration of the type of the delegate, that receive an array of classes of the type MyClass1
delegate void ProcessMyClass(MyClass1[] mc, int index);

The button1_Click () event handler demonstrates the use of an anonymous method that receives a class array as a parameter. Also is demonstrated the allocation of memory for an array of 10 instances of class objects.

 


Related topics