欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#初始化数组的方法小结

程序员文章站 2022-05-18 14:18:02
本文实例讲述了c#初始化数组的方法。分享给大家供大家参考,具体如下: c#声明数组并初始化,有三种方式。 对于一维数组: using system; usi...

本文实例讲述了c#初始化数组的方法。分享给大家供大家参考,具体如下:

c#声明数组并初始化,有三种方式。

对于一维数组:

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>第一种声明数组并初始化的方法:</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>第二种声明数组并初始化的方法:</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>第三种声明数组并初始化的方法:</b><br>");
    for (int i = 0; i < arrayc.length; i++)
    {
      string arr = arrayc[i];
      response.write("arrayc["+i+"] = "+arr + "<br>");
    }
  }
}

对于多维数组(以二维数组为例):

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>第一种声明数组并初始化的方法:</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>第二种声明数组并初始化的方法:</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>第二种声明数组并初始化的方法:</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>");
      }
    }
  }
}

更多关于c#相关内容感兴趣的读者可查看本站专题:《c#数组操作技巧总结》、《c#操作excel技巧总结》、《c#中xml文件操作技巧汇总》、《c#常见控件用法教程》、《winform控件用法总结》、《c#数据结构与算法教程》、《c#面向对象程序设计入门教程》及《c#程序设计之线程使用技巧总结

希望本文所述对大家c#程序设计有所帮助。