C # declares an array and initializes it in three ways.
For one-dimensional arrays:
Using System;
Using System.Data;
Using System.Configuration;
Using System.Web;
Using System.Web.Security;
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[] Arraya = {"Shirdrn", "Hamtty", "Saxery"};
Response.Write ("<b> the first method of declaring an array and initializing it:</b><br>");
for (int i = 0; i < arraya.length;i++)
{
String arr = Arraya[i];
Response.Write ("arraya[" + i + "] =" + arr + "<br>");
}
String[] Arrayb;
Arrayb = new string[3]{"Shirdrn", "Hamtty", "Saxery"};
Response.Write ("<b> second method of declaring arrays and initializing:</b><br>");
for (int i = 0; i < arrayb.length; i++)
{
String arr = Arrayb[i];
Response.Write ("arrayb[" + i + "] =" + arr + "<br>");
}
string[] Arrayc = new String[3];
Arrayc[0] = "SHIRDRN";
ARRAYC[1] = "Hamtty";
ARRAYC[2] = "Saxery";
Response.Write ("<b> third method of declaring arrays and initializing:</b><br>");
for (int i = 0; i < arrayc.length; i++)
{
String arr = Arrayc[i];
Response.Write ("arrayc[" +i+ "] =" +arr + "<br>");
}
}
}
For multidimensional arrays (take two-dimensional arrays as an example):
Using System;
Using System.Data;
Using System.Configuration;
Using System.Web;
Using System.Web.Security;
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[,] Multiarraya = {{"Shirdrn", "Hamtty", "Tuuty"}, {"New York", "Beijing", "Shanghai"}};
Response.Write ("<b> the first method of declaring an array and initializing:</b><br>");
for (int i = 0, i < Multiarraya.rank; i++)
{
for (int j = 0; J <= Multiarraya.getupperbound (Multiarraya . RANK-1); j + +)
{
String arr = multiarraya[i,j];
Response.Write ("multiarraya[" + i + "[" +j+ "] =" + arr + "<br>");
}
}
String[,] Multiarrayb = new string[2,3]{{"Shirdrn", "Hamtty", "Tuuty"}, {"New York", "Beijing", "Shanghai"}};
Response.Write ("<b> second method of declaring arrays and initializing:</b><br>");
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 + "<br>");
}
}
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 ("<b> second method of declaring arrays and initializing:</b><br>");
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>");
}
}
}
}