[C#基础]9.C#中的foreach 语法结构

Posted on 2009-09-26 12:39  Relax Active  阅读(1568)  评论(0)    收藏  举报
 

foreach语法结构,C#中新增的循环机制,可以迭代集合中的每一项

示例:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace Demo8

{

    class Program

    {

        static void Main(string[] args)

        {

            Program pro = new Program();

            pro.lay();

 

        }

        public void lay()

        {

            int[] num = new int[6] { 0, 1, 2, 3, 4, 5 };    //声明一个整型数组!

            foreach (int temp in num)      //foreach循环迭代num数组里的每一个数!

            {

                Console.WriteLine(temp);   //分别输出012345

            }

          

 

        }

    }

}

博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3