Реализовать IEnumerate и IEnumerable для собственного списка - C#
Формулировка задачи:
Помогите пожалуйста имплантировать IEnumerate и IEnumerable. Было задание так же без никакого yiled! Встрял, таращусь уже несколько часов. Помогите пожалуйста. Привожу весь код.
Листинг программы
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace MyList
- {
- sealed class MyList<T> : ICustomList<T>, IEnumerable<T>, IEnumerator<T>
- {
- #region ConstructorsAndVariables
- T[] array;
- public int Count { get; private set; }
- const int DefaultCapacity = 4;
- public MyList()
- {
- array = new T[DefaultCapacity];
- }
- public MyList(int capacity)
- {
- ThrowIfCapacityIsLessThanZero(capacity);
- array = new T[capacity];
- }
- #endregion ConstructorsAndVariables
- #region Implementation
- // actualy we can use Array.Copy and Array.Resize for more efficient
- private void ArrayCopy(T[] source, int sourceIndexStart, T[] destination, int destinationIndexStart, int length)
- {
- while (length != 0)
- {
- destination[destinationIndexStart++] = source[sourceIndexStart++];
- length--;
- }
- }
- private void ResizeArray()
- {
- T[] tempArray = new T[array.Length * 2];
- ArrayCopy(array, 0, tempArray, 0, Count);
- array = tempArray;
- }
- public void Add(T item)
- {
- if (Count == array.Length)
- ResizeArray();
- array[Count++] = item;
- }
- public void Insert(int index, T item)
- {
- ThrowIfIndexIsOutOfRange(index);
- if (Count == array.Length)
- ResizeArray();
- // harder way
- //T[] tempArray = new T[array.Length];
- //ArrayCopy(array, 0, tempArray, 0, index);
- //ArrayCopy(array, index, tempArray, index + 1, size - index);
- //array = tempArray;
- for (int i = Count; i > index; i--)
- array[i] = array[i - 1];
- array[index] = item;
- Count++;
- }
- public bool Remove(T item)
- {
- int indexOfFindItem = LinearSearch(item);
- if (indexOfFindItem == -1)
- return false;
- RemoveAt(indexOfFindItem);
- ArrayCopy(array, indexOfFindItem + 1, array, indexOfFindItem, Count - indexOfFindItem);
- Count--;
- return true;
- }
- public void RemoveAt(int index)
- {
- ThrowIfIndexIsOutOfRange(index);
- ArrayCopy(array, index + 1, array, index, Count - index);
- Count--;
- }
- public int IndexOf(T item)
- {
- return LinearSearch(item);
- }
- public void Reverse()
- {
- for (int i = 0; i < Count/2; i++)
- {
- T temp = array[i];
- array[i] = array[Count - i - 1];
- array[Count - i - 1] = temp;
- }
- }
- public T this[int index]
- {
- get
- {
- ThrowIfIndexIsOutOfRange(index);
- return array[index];
- }
- set
- {
- ThrowIfIndexIsOutOfRange(index);
- array[index] = value;
- }
- }
- public int LinearSearch(T item)
- {
- for (int i = 0; i < Count; i++)
- if (array[i].Equals(item))
- return i;
- // can't compare in this was array[i] == item
- // can't overload T == T, compile forbid this action
- // Generates generate code on run time that's why we didn't know which kind of type is T
- // Then use to compare EqualityComparer<T>.Default.Equals(array[i], item)
- return -1;
- }
- #endregion Implementation
- #region Exceptions
- private void ThrowIfCapacityIsLessThanZero(int capacity)
- {
- if(capacity < 0)
- throw new ArgumentOutOfRangeException("Capacity can't be less then zero");
- }
- private void ThrowIfIndexIsOutOfRange(int index)
- {
- if (index > Count || index < 0)
- throw new ArgumentOutOfRangeException("Index out of range");
- }
- #endregion Exceptions
- #region IEnumerableAndIEnumarte
- private int position = -1;
- public T Current
- {
- get
- {
- return Current;
- }
- }
- object IEnumerator.Current
- {
- get
- {
- return array[position];
- }
- }
- public bool MoveNext()
- {
- if (position < Count)
- {
- position++;
- return true;
- }
- else
- {
- Reset();
- return false;
- }
- }
- public void Reset()
- {
- position = -1;
- }
- public IEnumerator<T> GetEnumerator()
- {
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
- public void Dispose()
- {
- }
- #endregion IEnumerableAndIEnumarte
- }
- }
Решение задачи: «Реализовать IEnumerate и IEnumerable для собственного списка»
textual
Листинг программы
- MyList<int> t = new MyList<int>(3);
- t.Add(1);
- t.Add(2);
- t.Add(3);
- foreach (var i in t)
- {
- foreach (var j in t)
- {
- Console.WriteLine("{0},{1}", i,j);
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д