“Delegar” Respostas de código

Delegar

//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
PrashantUnity

Delegar

class Program
{
    delegate void Message(); // 1. Объявляем делегат
    static void Main()
    {
        Message mes;            // 2. Создаем переменную делегата
        mes = Hello;            // 3. Присваиваем этой переменной адрес метода
        mes();                  // 4. Вызываем метод
 
        void Hello() => Console.WriteLine("Hello METANIT.COM");
    }
}
Info Important

Respostas semelhantes a “Delegar”

Procure respostas de código populares por idioma

Procurar outros idiomas de código