C# for和 foreach 的數組遍歷 比較


     剛學習程序,感覺寫代碼 很有意思,所以把自己的感悟寫下來啦,第一次寫博客,可能是菜鳥中的菜鳥  時間久了,相信就會寫的很好哦! 

   for和 foreach 的數組遍歷 比較

    很簡單的程序,不解釋啦!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            int[] nums = { 3, 5, 99, 23, 53, 88 }; //定義數組
            int max = 0;
      
            for (int i = 0; i < nums.Length; i++)   //for遍歷數組
            {
                if (nums[i] > max)
                {
                    max = nums[i];
                }
            }
            Console.WriteLine("for遍歷數組demo:" + max);
            Console.WriteLine("====================================================");
            foreach (var p in nums)               //使用foreach 遍歷
            {
                if (p > max)
                {
                    max = p;
                }

            }
            Console.WriteLine("foreach遍歷數組demo:"+max);
           
           
            
        }
        
    }
}

   結果為:

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM