Skip to content

Instantly share code, notes, and snippets.

@kuanyingchou
Created July 28, 2014 11:41
Show Gist options
  • Save kuanyingchou/320eacb6eb3b2933d37f to your computer and use it in GitHub Desktop.
Save kuanyingchou/320eacb6eb3b2933d37f to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using System.Collections;
class Array
{
public static void Main()
{
//System.Console.WriteLine("hello, world");
Array array = new Array();
array.Add(3);
array.Add(5);
array.Add(7);
//======================
foreach(var i in array)
{
System.Console.WriteLine(i);
}
//======================
/*
IEnumrator e = array.GetEnumerator();
while(e.MoveNext())
{
System.Console.WriteLine(e.Current);
}
*/
//======================
}
}
class Array : IEnumerable
{
public int[] values;
public int index = 0;
public Array()
{
values = new int[10];
}
public void Add(int i)
{
//...
if(index == values.Length)
{
int newValues = new int[values.Length * 2];
//TODO...
}
values[index++] = i;
}
public IEnumerator GetEnumerator()
{
return new ArrayEnum(this);
}
//=================================
class ArrayEnum : IEnumerator
{
int i = -1;
Array arr;
public ArrayEnum(Array arr)
{
this.arr = arr;
}
public bool MoveNext()
{
if(i < arr.index - 1)
{
i++;
return true;
}
else
{
return false;
}
}
public object Current { get { return arr.values[i]; } }
public void Reset()
{
i = -1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment