Делегаты, разобрать простой пример - C#
Формулировка задачи:
Добрый день.
Почему выводит на экран только: "2"? Я ожидал увидеть: "12"
delegate int MyDel();
static void Main(string[] args)
{
MyDel del1 = () => 1;
MyDel del2 = () => 2;
MyDel del3 = del1 + del2;
Console.Write(del3());
Console.ReadLine();
}Решение задачи: «Делегаты, разобрать простой пример»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3 {
class Program {
delegate int MyDelegate();
static void Main(string[] args) {
MyDelegate del1 = () => 1;
MyDelegate del2 = () => 2;
MyDelegate del3 = del1 + del2;
int result = del3.GetInvocationList().Sum(d => ((MyDelegate)d).Invoke());
Console.WriteLine(result);
Console.ReadLine();
}
}
}