一个类型要想支持foreach则必须实现IEnumerable,IEnumerator两个接口。
2010-06-08 11:40
876 查看
一个类型要想支持foreach则必须实现IEnumerable,IEnumerator两个接口。
// Namespace: System.Collections, Library: BCL
public interface IEnumerable...{
IEnumerator GetEnumerator();
}
// Namespace: System.Collections, Library: BCL
public interface IEnumerator...{
bool MoveNext();
void Reset();
object Current ...{ get; }
}
实例:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Collections;
namespace Randam
{
public class car
{
string name;
int year;
public car(string str, int num)
{
name = str;
year = num;
}
public string Name
{
get
{
return name;
}
}
}
public class cars : IEnumerator,IEnumerable
{
private car[] carlist;
int position = -1;
//Create internal array in constructor.
public cars()
{
carlist= new car[6]
{
new car("Ford",1992),
new car("Fiat",1988),
new car("Buick",1932),
new car("Ford",1932),
new car("Dodge",1999),
new car("Honda",1977)
};
}
//IEnumerator and IEnumerable require these methods.
public IEnumerator GetEnumerator()
{
return (IEnumerator)this;
}
//IEnumerator
public bool MoveNext()
{
position++;
return (position < carlist.Length);
}
//IEnumerable
public void Reset()
{position = 0;}
//IEnumerable
public object Current
{
get
{
try
{
return carlist[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
public static void Main()
{
cars cars = new cars();
foreach (car s in cars)
{
Console.WriteLine("car’s value: {0}", s.Name);
}
IEnumerator ienum = (IEnumerator)cars.GetEnumerator();
ienum.Reset();
Console.WriteLine("***************************");
while (ienum.MoveNext())
{
car s = (car)ienum.Current;
Console.WriteLine("car’s value: {0}", s.Name);
}
}
}
}
// Namespace: System.Collections, Library: BCL
public interface IEnumerable...{
IEnumerator GetEnumerator();
}
// Namespace: System.Collections, Library: BCL
public interface IEnumerator...{
bool MoveNext();
void Reset();
object Current ...{ get; }
}
实例:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Collections;
namespace Randam
{
public class car
{
string name;
int year;
public car(string str, int num)
{
name = str;
year = num;
}
public string Name
{
get
{
return name;
}
}
}
public class cars : IEnumerator,IEnumerable
{
private car[] carlist;
int position = -1;
//Create internal array in constructor.
public cars()
{
carlist= new car[6]
{
new car("Ford",1992),
new car("Fiat",1988),
new car("Buick",1932),
new car("Ford",1932),
new car("Dodge",1999),
new car("Honda",1977)
};
}
//IEnumerator and IEnumerable require these methods.
public IEnumerator GetEnumerator()
{
return (IEnumerator)this;
}
//IEnumerator
public bool MoveNext()
{
position++;
return (position < carlist.Length);
}
//IEnumerable
public void Reset()
{position = 0;}
//IEnumerable
public object Current
{
get
{
try
{
return carlist[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
public static void Main()
{
cars cars = new cars();
foreach (car s in cars)
{
Console.WriteLine("car’s value: {0}", s.Name);
}
IEnumerator ienum = (IEnumerator)cars.GetEnumerator();
ienum.Reset();
Console.WriteLine("***************************");
while (ienum.MoveNext())
{
car s = (car)ienum.Current;
Console.WriteLine("car’s value: {0}", s.Name);
}
}
}
}
相关文章推荐
- 如何自己实现IEnumerable和IEnumerator接口以支持foreach语句
- 实现IEnumerable和IEnumerator接口以支持foreach语句
- 实现了IEnumerable接口的GetEnumerator 即可使用 Foreach遍历,返回一个IEnumerator对象
- 如何自己实现IEnumerable和IEnumerator接口以支持foreach语句
- 利用 IEnumerable接口 实现自定义类型的集合的foreach遍历
- [C#]实现IEnumerable接口来使用foreach语句的一个实例
- C# 通过IEnumberable接口和IEnumerator接口实现自定义集合类型foreach功能
- 如何实现IEnumerable<T>和IEnumerator接口,一个ASP.NET MVC日志模型的实现
- C#如何实现自定义集合的IEnumerable 和 IEnumerator 接口并使用foreach访问自定义集合
- 写一个算法,使对象可以像数组一样进行foreach循环,要求属性必须是私有的(Interator模式的PHP5实现,写一类实现Interator接口)
- 实践证明:当类想实现两个监听接口的时候,必须把两个都设置成内部类,不可能一个是外部类实现,一个是内部类实现。这样容易捕获错误,出现异常。
- Spring用@Autowire向一个类注入一个接口的两个实现类
- 能用foreach遍历访问的对象需要实现____接口或声明____方法的类型
- 如何使用反射确定一个属性是否实现了IList接口,如何确定元素量为空的集合的元素类型。
- java中,一个类实现某个接口,必须重写接口中的所有方法吗???
- 要使一个类支持 foreach 遍历,实现过程怎样?
- 编写一个Book类,该类至少有name和price两个属性。该类要实现Comarable接口,在接口的compareTo()方法中规定两个Book类实例的大小关系为二者的price属性的大小关系。
- 迭代器.NET实现—IEnumerable和IEnumerator (foreach实现)
- 《剑指Offer》用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。java
- 用两个栈来实现一个队列,完成队列的Push和Pop操作。队列中的元素为int类型。