Реализовать ienumerable для обобщенного класса - C#
Формулировка задачи:
Добрый день!
У меня есть два вопрос:
1. Помогите реализовать интерфейс в классе ienumerable MyList<T>.
2. Как можно сделать так, чтобы интерфейс IListable включал в себя индексатор. В задании говорится, что должен быть интерфейс, включающий в себя добавление элемента, индексатор и свойство для чтения.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _2
{
public interface IListable
{
int QuantityProp { get;}
// вот сюда индексатор
}
public class MyList<T>: IListable, IEnumerable<T>
{
int QuantityField;
T[] myArray;
public MyList(){}
public MyList(int QuantityField)
{
this.QuantityField = QuantityField;
myArray = new T[QuantityField];
}
public int QuantityProp
{
get { return QuantityField; }
}
public T this[int i] // этот индексатор в интерфейс
{
get { return myArray[i]; }
set { myArray[i] = value; }
}
public IEnumerator<T> GetEnumerator()
{
return myArray.GetEnumerator(); // выводит ошибку
}
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return myArray.GetEnumerator(); // выводит ошибку
}
}
class Program
{
static void Main(string[] args)
{
MyList<string> EX1 = new MyList<string>(2);
EX1[0] = "a";
EX1[1] = "b";
for (int i = 0; i<EX1.QuantityProp; i++)
Console.WriteLine(EX1[i]);
EX1[3] = "c";
foreach (string s in EX1)
Console.WriteLine(s);
Console.WriteLine(EX1.QuantityProp);
Console.ReadLine();
}
}
}Решение задачи: «Реализовать ienumerable для обобщенного класса»
textual
Листинг программы
public IEnumerator<T> GetEnumerator()
{
return (IEnumerator<T>)myArray.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return myArray.GetEnumerator();
}