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

来源:百度知道 编辑:UC知道 时间:2024/05/23 23:10:25
C#中要使用foreach需要实现什么接口或类
2) 能用foreach遍历访问的对象需要实现 ______________接口或声明______________方法的类型。
这个题目该怎样答?

能用foreach遍历访问的对象必须是集合或数组对象,而这些都是靠实现超级接口IEnumerator或被声明 GetEnumerator 方法的类型。

能用foreach遍历访问的对象需要实现 IEnumerator 接口或声明 GetEnumerator 方法的类型。

应该是 System.Collections.IEnumerable 或 System.Collections.Generic.IEnumerable<T>
请参阅微软官方文档:https://msdn.microsoft.com/zh-cn/library/ttw7t8t6.aspx

什么都不用实现的的,拿来就用就是的,
string[] arg=new string[5];
foreach(string str in arg)
{
Console.writeLine(str);
}

这样就遍历了string类型的数组

不用
就是循环遍历用的
跟for差不多