/// <summary>
/// 集合的接口
/// </summary>
public interface ICollection
{
int Count { get; }
/// <summary>
/// 获取迭代器
/// </summary>
/// <returns>迭代器</returns>
Iterator GetIterator();
}
/// <summary>
/// 迭代器接口
/// </summary>
public interface Iterator
{
bool MoveNext();
object Current { get; }
}
public class List : ICollection
{
private const int MAX = 10;
private object[] items;
public List()
{
items = new object[MAX];
}
public object this[int i]
{
get { return items[i]; }
set { this.items[i] = value; }
}
#region ICollection Members
public int Count
{
get { return items.Length; }
}
public Iterator GetIterator()
{
return new ListIterator(this);
}
#endregion
}
public class ListIterator : Iterator
{
private int index = 0;
private ICollection list;
public ListIterator(ICollection list)
{
this.list = list;
index = 0;
}
#region Iterator Members
public bool MoveNext()
{
if (index + 1 > list.Count)
return false;
else
{
index++;
return true;
}
}
public object Current
{
get { return list[index]; }
}
#endregion
}
/// <summary>
/// 测试
/// </summary>
public class Program
{
static void Main()
{
ICollection list = new List();
Iterator iterator = list.GetIterator();
while (iterator.MoveNext())
{
object current = iterator.Current;
}
}
}
public static void Main()
{
ArrayList list = new ArrayList();
list.Add(1);
list.Add(2);
list.Add(3);
foreach (object item in list)
{
Console.WriteLine(item.ToString());
}
}
.method private hidebysig static void Main() cil managed
{
.entrypoint
.maxstack 2
.locals init (
[0] class [mscorlib]System.Collections.ArrayList list,
[1] object item,
[2] class [mscorlib]System.Collections.IEnumerator CS$5$0000,
[3] class [mscorlib]System.IDisposable CS$0$0001)
L_0000: newobj instance void [mscorlib]System.Collections.ArrayList::.ctor()
L_0005: stloc.0
L_0006: ldloc.0
L_0007: ldc.i4.1
L_0008: box int32
L_000d: callvirt instance int32 [mscorlib]System.Collections.ArrayList::Add(object)
L_0012: pop
L_0013: ldloc.0
L_0014: ldc.i4.2
L_0015: box int32
L_001a: callvirt instance int32 [mscorlib]System.Collections.ArrayList::Add(object)
L_001f: pop
L_0020: ldloc.0
L_0021: ldc.i4.3
L_0022: box int32
L_0027: callvirt instance int32 [mscorlib]System.Collections.ArrayList::Add(object)
L_002c: pop
L_002d: ldloc.0
L_002e: callvirt instance class [mscorlib]System.Collections.IEnumerator [mscorlib]System.Collections.ArrayList::GetEnumerator()
L_0033: stloc.2
L_0034: br.s L_0048
L_0036: ldloc.2
L_0037: callvirt instance object [mscorlib]System.Collections.IEnumerator::get_Current()
L_003c: stloc.1
L_003d: ldloc.1
L_003e: callvirt instance string [mscorlib]System.Object::ToString()
L_0043: call void [mscorlib]System.Console::WriteLine(string)
L_0048: ldloc.2
L_0049: callvirt instance bool [mscorlib]System.Collections.IEnumerator::MoveNext()
L_004e: brtrue.s L_0036
L_0050: leave.s L_0063
L_0052: ldloc.2
L_0053: isinst [mscorlib]System.IDisposable
L_0058: stloc.3
L_0059: ldloc.3
L_005a: brfalse.s L_0062
L_005c: ldloc.3
L_005d: callvirt instance void [mscorlib]System.IDisposable::Dispose()
L_0062: endfinally
L_0063: call string [mscorlib]System.Console::ReadLine()
L_0068: pop
L_0069: ret
.try L_0034 to L_0052 finally handler L_0052 to L_0063
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有