foreach ()怎么实现啊,有什么功能

一个不小心就 2013-05-26 10:27:31
与Arraylist一起用还是?
...全文
170 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
一个不小心就 2013-05-29
  • 打赏
  • 举报
回复
懂了,谢谢你们
人生导师 2013-05-26
  • 打赏
  • 举报
回复
实现了实现 System.Collections.IEnumerable 或 System.Collections.Generic.IEnumerable<T> 接口的对象才可以使用foreach,具体如下:

static void Main(string[] args)
    {
        int[] fibarray = new int[] { 0, 1, 1, 2, 3, 5, 8, 13 };
        foreach (int element in fibarray)
        {
            System.Console.WriteLine(element);
        }
        System.Console.WriteLine();


        // Compare the previous loop to a similar for loop.
        for (int i = 0; i < fibarray.Length; i++)
        {
            System.Console.WriteLine(fibarray[i]);
        }
        System.Console.WriteLine();


        // You can maintain a count of the elements in the collection.
        int count = 0;
        foreach (int element in fibarray)
        {
            count += 1;
            System.Console.WriteLine("Element #{0}: {1}", count, element);
        }
        System.Console.WriteLine("Number of elements in the array: {0}", count);
    }
聖傑 2013-05-26
  • 打赏
  • 举报
回复
foreach功能和for差不多。 参考MSDN 感觉foreach更好用些
  • 打赏
  • 举报
回复
http://msdn.microsoft.com/zh-cn/library/vstudio/ttw7t8t6.aspx foreach 语句对实现 System.Collections.IEnumerable 或 System.Collections.Generic.IEnumerable<T> 接口的数组或对象集合中的每个元素重复一组嵌入式语句。 ArrayList可以使用foreach语句。

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧