简单的C#错误

简单的C#错误,c#,C#,这是我的代码: namespace ConsoleApplication3 { class Module1 { static void Main() { int i; int j; int[,] matriceFoglio; string stringa = ""; for (i = 1; (i <= 11); i++)

这是我的代码:

namespace ConsoleApplication3
{
  class Module1
  {
    static void Main()
    {
            int i;
            int j;
            int[,] matriceFoglio;
            string stringa = "";

            for (i = 1; (i <= 11); i++)
            {
                stringa = "";

                for (j = 1; (j <= 11); j++)
                {
                    matriceFoglio(i, j) = 0;
                    stringa = (stringa + matriceFoglio[i,j]);
                }

                Console.WriteLine(stringa);
            }

            Console.ReadLine();
            //  disegno l'albero di natale
            matriceFoglio[1, 5] = 1;
            matriceFoglio[2, 4] = 1;
            matriceFoglio[2, 5] = 1;
            matriceFoglio[2, 6] = 1;
            matriceFoglio[3, 3] = 1;
            matriceFoglio[3, 4] = 1;
            matriceFoglio[3, 5] = 1;
            matriceFoglio[3, 6] = 1;
            matriceFoglio[3, 7] = 1;
            matriceFoglio[4, 5] = 1;
            matriceFoglio[5, 5] = 1;

            //  disegno l'albero
            Console.WriteLine("");

            for (i = 1; (i <= 11); i++)
            {
                stringa = "";

                for (j = 1; (j <= 11); j++)
                {
                    stringa = (stringa + matriceFoglio[i, j]);
                }

                Console.WriteLine(stringa);
            }

            Console.ReadLine();
    }
  }
}
命名空间控制台应用程序3
{
类模块1
{
静态void Main()
{
int i;
int j;
int[,]matriceFoglio;
字符串stringa=“”;

对于(i=1;(i首先需要初始化阵列(本例中的大小为10x10):

其次,使用方括号访问数组,而不是使用花括号:

// replace matriceFoglio(i, j) = 0; with
matriceFoglio[i, j] = 0;
更新 我猜另一个错误与不正确的循环索引有关,基本上没有索引为
11
的元素(我假设数组大小为10x10)。它应该是:

for (i = 0; i < 10; i++)
{
    for (j = 0; j < 10; j++)
    {
    }
}
(i=0;i<10;i++)的

{
对于(j=0;j<10;j++)
{
}
}

您需要使用新的:

matriceFoglio = new int[10,10];
另外,当您为MatriceFolio赋值时,需要使用括号[],而不是括号,例如

matriceFoglio[i,j] = 0;

我知道其他人已经指出了这一点,但在此仅补充几点。这里有两个错误,这两个错误与编译器所说的基本相同。首先:

matriceFoglio(i, j) = 0;
这不是正确的语法-这是方法调用的语法,而不是数组赋值。这就是为什么编译器希望这是一个方法名。这实际上应该是

matriceFoglio[i, j] = 0;

另外,
int[,]MyReffoLogo;在使用时未初始化,因此编译器在这里也绝对正确。您必须在使用它之前创建数组。您不能只是开始分配给它。要理解这是为什么,首先考虑数组实际上是如何实现的:假设您要实现一个大小为10的数组。在数组中,运行时将在内存中“布局”10个连续位置,并存储第一个项的位置。数组索引是相对于初始地址的偏移量-这就是为什么第一个项是项0(根据定义,没有偏移量,第一个项位于指针位置)但是,如果您想访问数组中的第五项,它的位置是
(第一项的地址)+(32*4)
(假设为32位地址)。这就是为什么您可以进行固定时间随机访问-查找任意位置只是指针算术。

int[,]matriceflio
更改为
int[,]matriceFoglio=new int[10001000];
“为什么是1000?”,没有理由。是的,我已经这么做了,而且它是正确的,但是如果我启动调试,就会出现另一个错误。@Matteocolimedaglia我已经更新了我的答案,但我只是猜测而已。
matriceFoglio[i, j] = 0;