C#數組初始化的幾種方式


最怕你一生碌碌無為,還安慰自己平凡可貴

C#數組初始化的幾種方式

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

namespace OneArray
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arrOld;                                //數組初始化的幾種方式,注意賦值利用 {} 實現。
            arrOld=new int[5]{ 30, 32, 40, 25, 35 };       
            int[] arrOld1 = {30,20,212 };
            int[] arrOld2 = new int[4] { 1, 2, 3, 4 };
            int[] testArray=new int[10];
            for (int i = 0; i < 10; i++)
            {
                testArray[i] = i * 3;                    //對數組賦值前需要初始化數組長度
            }
            Console.WriteLine("員工年齡:");
            foreach (int n in arrOld)                     //使用foreach語句循環遍歷一維數組中的元素
            {
                Console.WriteLine("{0}", n + "歲 ");     //輸出數組中的元素
            }
            Console.ReadLine();
        }
    }
}

 試着修改一個數組的小demo,發現竟然改不出來,對語法不熟悉。編程還是要踏踏實實敲代碼,切記眼高手低,光看書是不行的。


免責聲明!

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



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