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;
{
tec) sender, EventArgs e)
{
string[] arrayA = { "Shirdrn", "Hamtty", "Saxery" };
");
for (int i = 0; i {
Response.Write("arrayA[" + i + "] = " + arr + "
");
}
string[] arrayB ;
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 {
}
}
}
對於多維數組(以二維數組為例):
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;
{
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[ 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中文網!