.NET 4.x IEnumerable && IEnumerator - C#
Формулировка задачи:
Доброго дня! Помогите разобраться с интерфейсами. К примеру, если посмотреть на сборку Array(mscorlib) то видно что он наследует интерфейс IEnumerable и реализует его метод GetEnumerator(). Вопрос куда делась реализация методов IEnumerator'а ?
namespace System.Collections
{
public interface IEnumerable
{
IEnumerator GetEnumerator();
}
public interface IEnumerator
{
object Current { get; }
bool MoveNext();
void Reset();
}
}Решение задачи: «.NET 4.x IEnumerable && IEnumerator»
textual
Листинг программы
using System;
using System.Collections;
[Serializable]
private sealed class ArrayEnumerator : IEnumerator, ICloneable
{
private Array array;
private int index;
private int endIndex;
private int startIndex;
private int[] _indices;
private bool _complete;
public object Current
{
get
{
if (this.index < this.startIndex)
{
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumNotStarted"));
}
if (this._complete)
{
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumEnded"));
}
return this.array.GetValue(this._indices);
}
}
internal ArrayEnumerator(Array array, int index, int count)
{
this.array = array;
this.index = index - 1;
this.startIndex = index;
this.endIndex = index + count;
this._indices = new int[array.Rank];
int num = 1;
for (int i = 0; i < array.Rank; i++)
{
this._indices[i] = array.GetLowerBound(i);
num *= array.GetLength(i);
}
this._indices[this._indices.Length - 1]--;
this._complete = (num == 0);
}
private void IncArray()
{
int rank = this.array.Rank;
this._indices[rank - 1]++;
for (int i = rank - 1; i >= 0; i--)
{
if (this._indices[i] > this.array.GetUpperBound(i))
{
if (i == 0)
{
this._complete = true;
return;
}
for (int j = i; j < rank; j++)
{
this._indices[j] = this.array.GetLowerBound(j);
}
this._indices[i - 1]++;
}
}
}
public object Clone()
{
return base.MemberwiseClone();
}
public bool MoveNext()
{
if (this._complete)
{
this.index = this.endIndex;
return false;
}
this.index++;
this.IncArray();
return !this._complete;
}
public void Reset()
{
this.index = this.startIndex - 1;
int num = 1;
for (int i = 0; i < this.array.Rank; i++)
{
this._indices[i] = this.array.GetLowerBound(i);
num *= this.array.GetLength(i);
}
this._complete = (num == 0);
this._indices[this._indices.Length - 1]--;
}
}