首頁 >後端開發 >C#.Net教程 >C#初始化數組的三種方式

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

高洛峰
高洛峰原創
2016-12-16 14:31:421834瀏覽

C#聲明數組並初始化,有三種方式。

對於一維數組:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System。 Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 

{
tec) sender, EventArgs e)
    {
        string[] arrayA = { "Shirdrn", "Hamtty", "Saxery" };
   
");
        for (int i = 0; i         {
         Response.Write("arrayA[" + i + "] = " + arr + "
");
        }

        string[] arrayB ;

         Response.Write("第二種宣告數組並初始化的方法:
");

        for (int i = 0; i                  Response.Write ("arrayB[" + i + "] = " + arr + "
");
        }

        string[] arrayC = new string[3];        arrayC[ 1] = "Hamtty";
        arrayC[2] = "Saxery";
        Response.Write("第三種宣告數組並初始化的方法:  Response.Write("第三個宣告數組並初始化的方法: >"); i = 0; i         {

            string  "+arr + "
");

        }
    }
}

對於多維數組(以二維數組為例):

using System;
using System.Data;
using System.Configuration;
using System.Web.Web; using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

public partial class _Default : System.Web. UI.Page 

{
    protected void Page_Load(object sender, EventArgs e)
    {
        string[,] mijti, "Hhtihati", "Huntio"), "Hijti"s", "Hijti"s", "Hijti"s", "Hijtis" "Shanghai" } };
        Response.Write("第一種宣告陣列並初始化的方法:
");
        for (int i = 0; i
        for (int i = 0; i i         {
            for (int j = 0; j              i + "]["+j+"] = " + arr + "
");

            }
        }

        string[,] multiArrayB = new string[2,3]{ { "Shirdrn", "Hamtty", "Tuuty" }, { "紐約", "北京", "上海" } }; ty" }, { "紐約", "北京 第二份報表且初始化的方法:
");
        for (int i = 0; i      multiArrayB.Rank; i++)
        {
            for (int j = 0; j                 string arr = multiArrayA[i, j];
       "multiArrayB[" + i + "][" + j + "] = " + arr + "
");
            }
      3];
multiArrayC[0,0] = "Shirdrn";

        multiArrayC[0,1] = "Hamtty";

       ";
        multiArrayC[ 1,1] = "北京";
multiArrayC[1,2] = "上海";
        Response.Write("宣告批次並初始化的方法:
");
   int i = 0 ; i         {
            for (int j.        {
                string arr = multiArrayA[i, j ];
                Response.Write("multiArrayC[" + i + "][" + j + "] = " no    }
}




更多C#初始化透明的方式文章請關注PHP中文網!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn