static void Main() { Test previous = new Test(6); List <Test> collection = new List <Test>(); FilterDelegate <Test> filter = Filter; SelectDelegate <Test> select = Select; for (int i = 0; i < 10; i++) { collection.Add(new Test(i)); } // use explicit types to be able to use ref in lambda CombinedDelegate <Test> combined = (IEnumerable <Test> c, ref Test p) => @select(filter(c), ref p); Test result = combined(collection, ref previous); //Expected result Test with Number = 7 }
public static void ShowCombinedDelegate() { //Создаем несколько экземпляров делегата типа CombinedDelegate CombinedDelegate countDelegateOne = CountToOne; CombinedDelegate countDelegateTwo = CountToTwo; CombinedDelegate countDelegateThree = CountToThree; //Комбинируем ссылки соданных делегатов на одном скомбинированом делегате CombinedDelegate combinedDelegate = countDelegateOne + countDelegateTwo + countDelegateThree; Console.WriteLine("Enter number from 1 to 5:"); string variantToRun = string.Empty; while (variantToRun != "exit") { variantToRun = Console.ReadLine(); switch (variantToRun) { case "1": countDelegateOne(); break; case "2": countDelegateTwo(); break; case "3": combinedDelegate(); break; case "4": //combinedDelegate = combinedDelegate - countDelegateTwo; combinedDelegate -= countDelegateTwo; combinedDelegate(); break; case "5": CombinedDelegate combinedDelegateTwoThree = combinedDelegate - countDelegateOne; combinedDelegateTwoThree(); break; default: variantToRun = "exit"; break; } } }