Viewing source for recipe1803cs.aspx

<%@ Page Language="C#" %>
<script runat="server">
void Page_Load()
{
	CustomCollection customCollection = new CustomCollection(10);
	
	DemoOutput.Text = "";
	foreach (CustomItem customItem in customCollection)
	{
	  DemoOutput.Text += customItem.Index + "<br />";
	}
}

public class CustomCollection : IEnumerable, IEnumerator
{
	private CustomItem[] customItems;
	private int current = -1;

	public CustomCollection(int Count)
	{
		customItems = new CustomItem[Count];
		for (int index = 0; index < Count; index++)
		{
			customItems[index] = new CustomItem(index);
		}
	}

	#region Implementation of IEnumerable
	public IEnumerator GetEnumerator()
	{
		return (IEnumerator) this;
	}
	#endregion

	#region Implementation of IEnumerator
	public void Reset()
	{
		current = -1;
	}

	public bool MoveNext()
	{
		if (current < customItems.Length - 1)
		{
			current++;
			return true;
		}
		else
		{
			return false;
		}
	}

	public object Current
	{
		get
		{
			return customItems[current];
		}
	}
	#endregion
}

public class CustomItem
{
	private int index;
	
	public int Index
	{
		get
		{
			return index;
		}
	}
	
	public CustomItem(int Index)
	{
		index = Index;
	}
}
</script>
<html>
	<head>
		<title>Creating a Custom Collection</title>
	</head>
	<body>
		<form id="MainForm" runat="server">
			Output of Looping through a Custom Collection
			<br />
			<asp:literal id="DemoOutput" runat="server" />
		</form>
	</body>
</html>