Интерфейсы и делегаты. Каков функционал IEnumerable в приведенном коде - C#
Формулировка задачи:
пробую так, но не очень то получается
и прошу объяснить при чем тут IEnumerable
Листинг программы
- namespace ConsoleApplication
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<String> list = new List<String>();
- list.Add("2");
- list.Add("3");
- Select<List<String>>(/*что передать первым аргументом?*/, ,delegate(List<string> item) {
- for (int i = 0; i<item.Count;i++) {//производим действия над коллекцией }
- return item;//возвращаем новую коллекцию
- }
- }
- // public T d<T>(T item) { return item; }
- public static IEnumerable<T> Select<T>(this IEnumerable<T> collection, MyFunc<T> selector) {
- //тут надо вызвать selector(collection)?
- };
- public delegate T MyFunc<T>(T item);
- }
- }
Решение задачи: «Интерфейсы и делегаты. Каков функционал IEnumerable в приведенном коде»
textual
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplication37
- {
- static class Program
- {
- public delegate T MyFunc<T>(T item)where T:struct;
- static void Main(string[] args)
- {
- //Экземпляр делегата с указанием цели
- MyFunc<int> myFunc = new MyFunc<int>(Multi);
- //Коллекция, над которой совершается преобразование
- int[] myArr = { 1, 4, 7, 2, 9, 5, 2, 0, 7, 2, 7 };
- //Преобразованная коллекция
- int[] newArr = (myArr.MySelect(myFunc)).ToArray();
- //Вывод полученной коллекции
- foreach (int i in newArr)
- Console.WriteLine("Item: {0}", i);
- Console.ReadLine();
- }
- //Метод-расширение
- public static IEnumerable<T> MySelect<T>(this IEnumerable<T> collection, MyFunc<T> selector)where T:struct
- {
- IEnumerable<T> coll = from n in collection select (selector(n));
- return coll;
- }
- //Метод для делегата
- static int Multi(int x)
- {
- return x * 10;
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д