SoFunction
Updated on 2025-04-06

Summary of the method of initializing array in C#

This article describes the method of initializing an array in C#. Share it for your reference, as follows:

There are three ways to declare an array and initialize it.

For one-dimensional arrays:

using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
public partial class _Default : 
{
  protected void Page_Load(object sender, EventArgs e)
  {
    string[] arrayA = { "Shirdrn", "Hamtty", "Saxery" };
    ("<b>The first method to declare an array and initialize it:</b><br>");
    for (int i = 0; i &lt; ;i++ )
    {
      string arr = arrayA[i];
      ("arrayA[" + i + "] = " + arr + "&lt;br&gt;");
    }
    string[] arrayB ;
    arrayB = new string[3]{ "shirdrn", "Hamtty", "Saxery" };
    ("<b>The second method of declaring an array and initializing it:</b><br>");
    for (int i = 0; i &lt; ; i++)
    {
      string arr = arrayB[i];
      ("arrayB[" + i + "] = " + arr + "&lt;br&gt;");
    }
    string[] arrayC = new string[3];
    arrayC[0] = "Shirdrn";
    arrayC[1] = "Hamtty";
    arrayC[2] = "Saxery";
    ("<b>The third method of declaring an array and initializing it:</b><br>");
    for (int i = 0; i &lt; ; i++)
    {
      string arr = arrayC[i];
      ("arrayC["+i+"] = "+arr + "&lt;br&gt;");
    }
  }
}

For multi-dimensional arrays (taking two-dimensional arrays as an example):

using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
public partial class _Default : 
{
  protected void Page_Load(object sender, EventArgs e)
  {
    string[,] multiArrayA = { { "Shirdrn", "Hamtty", "Tuuty" }, { "New York", "Beijing", "Shanghai" } };
    ("<b>The first method to declare an array and initialize it:</b><br>");
    for (int i = 0; i &lt; ; i++)
    {
      for (int j = 0; j &lt;= (-1);j++ )
      {
        string arr = multiArrayA[i,j];
        ("multiArrayA[" + i + "]["+j+"] = " + arr + "&lt;br&gt;");
      }
    }
    string[,] multiArrayB = new string[2,3]{ { "Shirdrn", "Hamtty", "Tuuty" }, { "New York", "Beijing", "Shanghai" } };
    ("<b>The second method of declaring an array and initializing it:</b><br>");
    for (int i = 0; i &lt; ; i++)
    {
      for (int j = 0; j &lt;= ( - 1); j++)
      {
        string arr = multiArrayA[i, j];
        ("multiArrayB[" + i + "][" + j + "] = " + arr + "&lt;br&gt;");
      }
    }
    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";
    ("<b>The second method of declaring an array and initializing it:</b><br>");
    for (int i = 0; i &lt; ; i++)
    {
      for (int j = 0; j &lt;= ( - 1); j++)
      {
        string arr = multiArrayA[i, j];
        ("multiArrayC[" + i + "][" + j + "] = " + arr + "&lt;br&gt;");
      }
    }
  }
}

For more information about C#, please visit the special topic of this site:Summary of C# array operation skills》、《Summary of C# operation skills》、《Summary of XML file operation skills in C#》、《Tutorial on the usage of common C# controls》、《Summary of WinForm control usage》、《C# data structure and algorithm tutorial》、《Introduction to C# object-oriented programming tutorial"and"Summary of thread usage techniques for C# programming

I hope this article will be helpful to everyone's C# programming.