グローバルを宣言することはc#にとって重要ではないことを私は知っています。そして、私は多次元配列用のこのコードを持っています。ここでは、ユーザーに行数を入力してから、各行の列数を入力してもらいます。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Multidimensional_Array
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("ENter the no of rows ");
int rows=Convert.ToInt32(Console.ReadLine());
for(int i=0;i<rows;i++)
{
Console.WriteLine("Enter no of columns for "+i+" row");
var columns = Convert.ToInt32(Console.ReadLine());
int [,] multiDynamic=new int[rows,columns];
Console.WriteLine("enter " +i+ " row elements");
for(int j=0;j<columns;j++)
{
multiDynamic[i,j]=Convert.ToInt32(Console.ReadLine());
}
}
Console.WriteLine("The array elements are ");
}
}
配列を表示するためのコードを書いてみたところ、配列のスコープがmultiDynamic
forループ内に残っていることがわかりました。この配列にアクセスする方法はありますか?
更新 -最後columns
に配列を表示しているときに、配列にもアクセスする必要があります。これについても解決策を投稿してください。