C#中要使用foreach需要实现什么接口或类

C#中要使用foreach需要实现什么接口或类
2) 能用foreach遍历访问的对象需要实现 ______________接口或声明______________方法的类型。
这个题目该怎样答?

能用foreach遍历访问的对象必须是集合或数组对象,而这些都是靠实现超级接口IEnumerator或被声明 GetEnumerator 方法的类型。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2007-12-10
能用foreach遍历访问的对象需要实现 IEnumerator 接口或声明 GetEnumerator 方法的类型。
第2个回答  2016-10-11
应该是 System.Collections.IEnumerable 或 System.Collections.Generic.IEnumerable<T>
请参阅微软官方文档:https://msdn.microsoft.com/zh-cn/library/ttw7t8t6.aspx
第3个回答  2007-12-10
什么都不用实现的的,拿来就用就是的,
string[] arg=new string[5];
foreach(string str in arg)
{
Console.writeLine(str);
}

这样就遍历了string类型的数组
第4个回答  2007-12-10
不用
就是循环遍历用的
跟for差不多