You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mchsamples-.net-core/ex_023_003_IEnumerator_ex3/StrangeCollection.cs

75 lines
2.0 KiB

// ========================================================================
//
// Copyright (C) 2016-2017 MARC CHEVALDONNE
// marc.chevaldonne.free.fr
//
// Module : StrangeCollection.cs
// Author : Marc Chevaldonné
// Creation date : 2016-09-28
//
// ========================================================================
using System;
using System.Collections.Generic;
namespace ex_023_003_IEnumerator_ex3
{
class StrangeCollection : IEnumerable<int>
{
int[] data = { 1, 2, 3, 4, 5, 6 };
public IEnumerator<int> GetEnumerator()
{
return new StrangeEnumerator(this);
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return new StrangeEnumerator(this);
}
class StrangeEnumerator : IEnumerator<int>
{
int mCurrentIndex = -1;
StrangeCollection mCollection;
internal StrangeEnumerator(StrangeCollection collection)
{
mCollection = collection;
}
public int Current
{
get
{
return mCollection.data[mCurrentIndex];
}
}
object System.Collections.IEnumerator.Current
{
get
{
return Current;
}
}
public bool MoveNext()
{
mCurrentIndex++;
while (mCurrentIndex < mCollection.data.Length && mCollection.data[mCurrentIndex] % 2 != 0)
{
mCurrentIndex++;
}
return mCurrentIndex < mCollection.data.Length;
}
public void Reset()
{
mCurrentIndex = -1;
}
void IDisposable.Dispose() { }
}
}
}