B
B
BadCats2016-04-12 15:05:18
C++ / C#
BadCats, 2016-04-12 15:05:18

Question about the delegate class and the parameters it takes?

hello everyone, just started learning about delegates. there is this example:

// Класс, метод которого будет сообщен с делегатом.
    static class MyClass
    {
        // Создаем статический метод, который планируем сообщить с делегатом.
        public static void Method()
        {
            Console.WriteLine("Строку вывел метод сообщенный с делегатом.");
        }
    }

    // На 21-й строке создаем класс-делегата с именем MyDelegate,
    // метод, который будет сообщен с экземпляром данного класса-делегата, 
    // не будет ничего принимать и не будет ничего возвращать.

    public delegate void MyDelegate();  // Создаем класс делегата. (1) 

    class Program
    {
        static void Main()
        {
            MyDelegate myDelegate = new MyDelegate(MyClass.Method); // Создаем экземпляр делегата. (2)

            myDelegate.Invoke(); // Вызываем метод сообщенный с делегатом. (3)

            myDelegate();        // Другой способ вызова метода сообщенного с делегатом. (3')

            // Delay.
            Console.ReadKey();
        }
    }

pay attention to this line:
public delegate void MyDelegate();  // Создаем класс делегата. (1)

this course is provided in the form of video tutorials, the author says the following:
"note that the method (the default constructor in our delegate class) that will be reported with this instance of this delegate class - should not accept anything , and also nothing should return "
here is my question: what is the default constructor of a delegate class : should take nothing , and also should return nothing " - is it an axiom for all delegates when they are created , or is it a specific case due to the fact that the given delegate, in this case, communicated with this method:
public static void Method()
        {
            Console.WriteLine("Строку вывел метод сообщенный с делегатом.");
        }

- which does not take anything as parameters and is void, i.e. returns nothing, and did this method affect the principles of constructing a delegate and the parameters of its constructor?
That is, in the end it is either
  • general axiom
  • a specific case of constructing a delegate class reported with a given method

here, as an answer, I want to get an explanation and an answer, which of the two options is correct.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
atticus_finch, 2016-04-12
@BadCats

Answering your question - this is a specific case of constructing a delegate class reported with a given method.
When you create a delegate class ( public delegate void MyDelegate();) you specify the signature of methods that can then be communicated with that delegate.
As an example, here is a delegate with accepted parameters and a return value.
You can now communicate methods with the same signature with this delegate. for example

public static int Sum(int a, int b)
        {
            return a+b;
        }

Delegate call
class Program
    {
        static void Main()
        {
            MyDelegate myDelegate = new MyDelegate(MyClass.Sum); 
            int result = myDelegate(5, 10);
        }
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question