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,发现竟然改不出来,对语法不熟悉。编程还是要踏踏实实敲代码,切记眼高手低,光看书是不行的。

posted on 2017-09-07 15:01  stringAdmin  阅读(44402)  评论(0编辑  收藏  举报

导航