Heim >Backend-Entwicklung >C#.Net-Tutorial >Drei Möglichkeiten, Arrays in C# zu initialisieren
C# deklariert und initialisiert Arrays auf drei Arten.
Für eindimensionales Array:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System . Web.Security;
mit System.Web.UI;
mit System.Web.UI.WebControls;
mit System.Web.UI.WebControls.WebParts;
mit System.Web.UI . HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string[] arrayA = { "Shirdrn", "Hamtty", "Saxery" };
Response.Write("Die erste Möglichkeit, ein Array zu deklarieren und zu initialisieren:
");
for (int i = 0; i < arrayA.Length;i++ )
arr + "
");
}
string[] arrayB ;
{
string arr = arrayB[i];
Response.Write("arrayB[" + i + "] = " + arr + "
");
}
string[] arrayC = new string[3];
arrayC[0] = "Shirdrn";
arrayC[1] = "Hamtty ";
Response.Write("Die dritte Möglichkeit, ein Array zu deklarieren und zu initialisieren:
");
for (int i = 0; i < arrayC.Length; i++)
string arr = arrayC[i]; System.Configuration;
using System.Web;
mit System.Web. Sicherheit;
mit System.Web.UI;
mit System.Web.UI.WebControls;
mit System.Web.UI.WebControls.WebParts;
mit System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
{
string[,] multiArrayA = { { "Shirdrn", "Hamtty", "Tuty" }, { "New York", "Beijing", "Shanghai" } }; Response.Write("Die erste Möglichkeit, ein Array zu deklarieren und zu initialisieren :
");
i++)
{
for (int j = 0; j <= multiArrayA.GetUpperBound(multiArrayA.Rank-1);j++ )
{
];
Response.Write("multiArrayA[" + i + "]["+j+"] = " + arr + "
");
string[,] multiArrayB = neuer String[2,3]{ { "Shirdrn", "Hamtty", "Tuty" }, { "New York", "Beijing", "Shanghai" } };
Response.Write("第二种声明数组并初始化的方法:
");
for (int i = 0; i < multiArrayB.Rank; i++)
{
for (int j = 0; j <= multiArrayB.GetUpperBound(multiArrayB.Rank - 1); j++)
{
string arr = multiArrayA[i, j];
Response.Write("multiArrayB[" + i + "][" + j + "] = " + arr + "
");
}
}
string[ ,] multiArrayC = new string[2, 3];
multiArrayC[0,0] = "Shirdrn";
multiArrayC[0,1] = "Hamtty";
multiArrayC[0,2] = "Tuuty";
multiArrayC[1,0] = "New York";
multiArrayC[1,1] = "Beijing";
multiArrayC[1,2] = "Shanghai";
Response.Write("第二种声明数组并初始化的方法:
");
for (int i = 0; i < multiArrayC.Rank; i++)
{
for (int j = 0; j <= multiArrayC.GetUpperBound(multiArrayC.Rank - 1); j++)
{
string arr = multiArrayA[i, j];
Response.Write("multiArrayC[" + i + "][" + j + "] = " + arr + "< br>");
}
三种方式相关文章请关注PHP中文网!