Реализовать ienumerable для обобщенного класса - C#

Узнай цену своей работы

Формулировка задачи:

Добрый день! У меня есть два вопрос: 1. Помогите реализовать интерфейс в классе ienumerable MyList<T>. 2. Как можно сделать так, чтобы интерфейс IListable включал в себя индексатор. В задании говорится, что должен быть интерфейс, включающий в себя добавление элемента, индексатор и свойство для чтения.
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace _2
  7. {
  8. public interface IListable
  9. {
  10. int QuantityProp { get;}
  11. // вот сюда индексатор
  12. }
  13. public class MyList<T>: IListable, IEnumerable<T>
  14. {
  15. int QuantityField;
  16. T[] myArray;
  17. public MyList(){}
  18. public MyList(int QuantityField)
  19. {
  20. this.QuantityField = QuantityField;
  21. myArray = new T[QuantityField];
  22. }
  23. public int QuantityProp
  24. {
  25. get { return QuantityField; }
  26. }
  27. public T this[int i] // этот индексатор в интерфейс
  28. {
  29. get { return myArray[i]; }
  30. set { myArray[i] = value; }
  31. }
  32. public IEnumerator<T> GetEnumerator()
  33. {
  34. return myArray.GetEnumerator(); // выводит ошибку
  35. }
  36. IEnumerator<T> IEnumerable<T>.GetEnumerator()
  37. {
  38. return myArray.GetEnumerator(); // выводит ошибку
  39. }
  40.  
  41. }
  42. class Program
  43. {
  44. static void Main(string[] args)
  45. {
  46. MyList<string> EX1 = new MyList<string>(2);
  47. EX1[0] = "a";
  48. EX1[1] = "b";
  49. for (int i = 0; i<EX1.QuantityProp; i++)
  50. Console.WriteLine(EX1[i]);
  51. EX1[3] = "c";
  52. foreach (string s in EX1)
  53. Console.WriteLine(s);
  54. Console.WriteLine(EX1.QuantityProp);
  55. Console.ReadLine();
  56. }
  57. }
  58. }

Решение задачи: «Реализовать ienumerable для обобщенного класса»

textual
Листинг программы
  1.         public IEnumerator<T> GetEnumerator()
  2.         {
  3.             return (IEnumerator<T>)myArray.GetEnumerator();
  4.         }
  5.  
  6.         IEnumerator IEnumerable.GetEnumerator()
  7.         {
  8.             return myArray.GetEnumerator();
  9.         }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

11   голосов , оценка 4.364 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы