当前位置:编程学习 > C#/ASP.NET >>

谁能简单说下foreach用法

谁能简单说下foreach用法,最好举些例子 --------------------编程问答-------------------- foreach用于迭代循环一个集合、列表、数组等对象,用于获得每一个元素。
这是官方API文档http://msdn.microsoft.com/zh-cn/library/ttw7t8t6(v=vs.100).aspx
static void Main(string[] args)
        {
            int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };//定义1个整型数组

            foreach (int item in array)
            {
                Console.WriteLine(item);//foreach循环输出整型数组每一项
            }
        }
--------------------编程问答-------------------- foreach (迭代变量 in 集合)
{
    循环体
} --------------------编程问答-------------------- 循环  --------------------编程问答-------------------- 2楼表达得够清楚只有一种用法吗 --------------------编程问答--------------------
引用 4 楼 ljhestfirst 的回复:
2楼表达得够清楚只有一种用法吗

方法就是这一种,不过数组,List,Map,数组对象都可以foreach的。
和for各有优缺点 --------------------编程问答-------------------- 就是循环读取
foreach(声明变量 in 需要循环读取的数组名字)
{
      然后你就可以操作你声明的变量了
} --------------------编程问答-------------------- 下面的for循环和foreach其实是一样的

int[] a = new int[] {1,2,3,4,5,6,7,8,9,0};
for(int i;i < 10;i++) {
    Console.WriteLine("{0}",a[i]);
}
foreach(int i in a) {
    Console.WriteLine("{0}",i);
}
--------------------编程问答--------------------
引用 4 楼 ljhestfirst 的回复:
2楼表达得够清楚只有一种用法吗

还有一种用法,就是只有一条语句的情况下,可以省略中括号。 --------------------编程问答--------------------
引用 7 楼 okbianhao 的回复:
下面的for循环和foreach其实是一样的

int[] a = new int[] {1,2,3,4,5,6,7,8,9,0};
for(int i;i < 10;i++) {
    Console.WriteLine("{0}",a[i]);
}
foreach(int i in a) {
    Console.WriteLine("{0}",i);
}

正解 --------------------编程问答-------------------- 给你写一个例子:
using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    static class Program
    {
        static void Main(string[] args)
        {
            var lst = new MyList();
            foreach (var x in lst)
                Console.WriteLine(x);
            Console.ReadLine();
        }

    }

    public class MyList : IEnumerable<int>
    {
        public int num;

        public IEnumerator<int> GetEnumerator()
        {
            return new Enumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }

        public class Enumerator : IEnumerator<int>
        {
            private int x = 1;

            public int Current
            {
                get { return x; }
            }

            public void Dispose()
            {
            }

            object System.Collections.IEnumerator.Current
            {
                get { return Current; }
            }

            public bool MoveNext()
            {
                x += 2;
                if (x >= 100)
                    return false;

                return true;
            }

            public void Reset()
            {
                x = 1;
            }
        }
    }
}


会抄别人的一两种foreach代码处理一个整数数组的代码,只能说明你会抄一两种代码。而这根本不是学习!

要学习,就要这样学——搞懂基本的原理。 --------------------编程问答-------------------- 搞懂了原理以后,你再看 Array 类型定义
	public abstract class Array : ICloneable, IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable
{

一看就知道“哦,它实现了 IEnmerable接口,因此与其它数百个类型对象一样,凡是Array都可以用于foreach”! --------------------编程问答--------------------
namespace Test
{
   public class CollectionTest (ICollection ic)
   {
         foreach (object ob in ic)
         {
                Console.Write("the result is {0}",ob.ToString());
         }
    }

} --------------------编程问答-------------------- 其实你说的太笼统了, 什么叫foreach的用法?

foreach 本身就非常简单, 只要是实现了 IEnumerable 接口的对象都可以使用 foreach 来遍历。

用来代替for 或其它循环关键字会更高效。 --------------------编程问答-------------------- foreach 块的任何点使用 break 关键字跳出循环,或使用 continue 关键字直接进入循环的下一轮迭代,foreach 循环还可以通过 goto、return 或 throw 语句退出
pubic class For
{
    static void Main(string[] args)
    {
        int[] a= new int[] { 0, 1, 2, 3, 5, 8, 13 };
        foreach (int i in a)
        {
            System.Console.WriteLine(i);
        }
    }
}
输出的结果是0
1
2
3
5
8
13 --------------------编程问答-------------------- 除 --------------------编程问答--------------------
引用 1 楼 guwei4037 的回复:
foreach用于迭代循环一个集合、列表、数组等对象,用于获得每一个元素。
这是官方API文档http://msdn.microsoft.com/zh-cn/library/ttw7t8t6(v=vs.100).aspx
static void Main(string[] args)
        {
            int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };//定义1个整型数组

            foreach (int item in array)
            {
                Console.WriteLine(item);//foreach循环输出整型数组每一项
            }
        }
+1
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,