Three ways to initialize an array in C #

Source: Internet
Author: User
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>");
}
}


}
}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.