Question : Delegate
Answered by : prashant-priyadarshi
//A delegate is an object that knows how to call a method.
delegate int Transformer (int x);
//Transformer is compatible with any method
//with an int return type and a single int parameter,
int Square (int x)
{ return x * x;
}
//Or, more tersely:
int Square (int x) => x * x;
//Assigning a method to a delegate variable creates a delegate instance:
Transformer t = Square;
//You can invoke a delegate instance in the same way as a method:
int answer = t(3); // answer is 9
Source : https://learning.oreilly.com/library/view/c-10-in/9781098121945/ch04.html | Last Update : Wed, 25 May 22
Question : Delegate
Answered by : info-important
class Program
{ delegate void Message(); // 1. Объявляем делегат static void Main() { Message mes; // 2. Создаем переменную делегата mes = Hello; // 3. Присваиваем этой переменной адрес метода mes(); // 4. Вызываем метод void Hello() => Console.WriteLine("Hello METANIT.COM"); }
}
Source : | Last Update : Fri, 18 Mar 22