How to initialize an array in C # summary _c# tutorial

Source: Internet
Author: User

The example in this article describes the method of C # initialization arrays. Share to everyone for your reference, specific as follows:

C # declares an array and initializes it in three different 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 to declare an array and initialize 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> the second method of declaring an array and initializing it:</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> the third method of declaring an array and initializing it:</b><br>");
      for (int i = 0; i < arrayc.length i++) {string arr = arrayc[i];
    Response.Write ("arrayc[" +i+ "] =" +arr + "<br>");

 }
  }
}

For multidimensional arrays (for example, in two-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
    [,] Multiarraya = {{"Shirdrn", "Hamtty", "Tuuty"}, {"New York", "Beijing", "Shanghai"}};
    Response.Write ("<b> the first method to declare an array and initialize it:</b><br>"); for (int i = 0; i < Multiarraya.rank i++) {for (int j = 0; J <= Multiarraya.getupperbound (Multiarraya.ra
        NK-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> the second method of declaring an array and initializing it:</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> the second method of declaring an array and initializing it:</b><br>"); for (int i = 0; i < Multiarrayc.rank i++) {for (int j = 0; J <= Multiarrayc.getupperbound (Multiarrayc.ra NK-1);
        J + +) {string arr = Multiarraya[i, j];
      Response.Write ("multiarrayc[" + i + "] [" + j + "] =" + arr + "<br>");

 }
    }
  }
}

Read more about C # Interested readers can view the site topics: "C # array Operation tips Summary", "C # Operation Excel Skills Summary", "C # XML file Operation Tips Summary", "C # Common control usage Tutorial", "WinForm Control Usage Summary", "C # tutorial on data structure and algorithms, Introduction to C # object-oriented programming, and a summary of thread usage tips for C # programming

I hope this article will help you with C # programming.

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.