hello everyone, just started to teach delegates. and while I understood the following:
in order to create a delegate, we first create a delegate class (yes, yes, I know that this is not an entirely correct name)
public delegate void MyDelegate(); // Создаем класс делегата. (1) The delegate class describes the signature of the method that we communicate (we make common from the word) with this delegate.
// Создаем статический метод, который планируем сообщить с делегатом. public static void Method() { Console.WriteLine("Строку вывел метод сообщенный с делегатом."); } then, we create an instance of the delegate whose constructor accepts the method we need for the message:
MyDelegate myDelegate = new MyDelegate(MyClass.Method); // Создаем экземпляр делегата. (2) since I understood if the signature of another method
// Создаем статический метод, который планируем сообщить с делегатом. public static void MyMethod() { Console.WriteLine("Строку вывел метод сообщенный с делегатом."); } - matches the delegate class signature, we only need to create another delegate instance
MyDelegate myDelegate = new MyDelegate(MyClass.MyMethod); // Создаем экземпляр делегата. (2) and just as a constructor parameter, pass another method with the same signature (except the name of course)
And if the signature of the method we want to communicate differs from the delegate class already existing in the program, then we need to create a new one that describes another signature of the method we need (type of returned and received values) and then an instance of this delegate class.
Ie as I understood, if the signature of two or three methods in the program is the same (except for the name), then we can simply “clip” delegates instances, each of which will be communicated with its own of these several methods. And if three methods with the same signature, and the fourth - with excellent, for this you will have to create a new delegate class.
I understand everything correctly?
MyDelegate myDelegate = MyClass.Method;- Alexey Shimansky