Exemplo n.º 1
0
        static void Main(string[] args)
        {
            PrintNameDelegate printNameDelegate = new PrintNameDelegate(PrintName);

            printNameDelegate("Atanas");

            Console.WriteLine("Result when pass the delegate as parameter to method.");
            PrintPersonInfo(printNameDelegate);

            Console.WriteLine("Store more than one method in delegate");
            CalculatorDelegate calculatorDelegate = Sum;

            calculatorDelegate += Multiply;
            calculatorDelegate += Divide;
            calculatorDelegate -= Multiply;
            calculatorDelegate(100, 20);

            Console.WriteLine();
            Console.WriteLine("Use annonymous function");
            calculatorDelegate += (double firstNumber, double secondNumber) => Console.WriteLine($"{firstNumber} * {secondNumber} = {firstNumber * secondNumber}");
            calculatorDelegate(20, 5);
        }
Exemplo n.º 2
0
 private static void PrintPersonInfo(PrintNameDelegate printNameDelegate)
 {
     printNameDelegate("Atanas");
     Console.WriteLine("I am 18 years old");
 }
    private PrintNameDelegate GiveMyDelegate()
    {
        PrintNameDelegate pnd = PrintNameMethod;

        return(pnd);
    }