下面资料是关于C# foreach语句遍历集合类型演示范例的内容,希望能对各位朋友有些用处。
using System;
using System.Collections;
public class Tokens: IEnumerable
{
private string[] elements;
Tokens(string source, char[] delimiters)
{
elements = source.Split(delimiters);
}
{
return new TokenEnumerator(this);
}
{
return (IEnumerator) new TokenEnumerator(this);
}
public class TokenEnumerator: IEnumerator
{
private int position = -1;
private Tokens t;
public TokenEnumerator(Tokens t)
{
this.t = t;
}
public bool MoveNext()
{
if (position < t.elements.Length - 1)
{
position++;
return true;
}
else
{
return false;
}
}
public void Reset()
{
position = -1;
}
{
get
{
return t.elements[position];
}
}
{
get
{
return t.elements[position];
}
}
}
static void Main()
{
Tokens f = new Tokens("This is a well-done program.",
new char [] {‘ ‘,‘-‘});
{
Console.WriteLine(item);
}
}
}
原文地址:https://blog.51cto.com/14314834/2388691
时间: 2024-10-09 14:29:28