“Déléguer” Réponses codées

Déléguer

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

Déléguer

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

Réponses similaires à “Déléguer”

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code