C# C试图打印数组时出现IndexOutOfRangeAcception错误

C# C试图打印数组时出现IndexOutOfRangeAcception错误,c#,arrays,C#,Arrays,这里是新的c程序员 我目前正在制作一个简单的程序,打印出一个数字的前10个乘法表。这是我的代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Times_tables_calculator { class Program { static void Main

这里是新的c程序员

我目前正在制作一个简单的程序,打印出一个数字的前10个乘法表。这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Times_tables_calculator
{
    class Program
    {
        static void Main(string[] args)
        {
            int number;
            int counter;
            int timestable;
            int[] TimeTableList = new int[10];
            counter = 0;
            Console.WriteLine("Enter a number:");
            number = int.Parse(Console.ReadLine());

            while (counter <= 10) 
            {
                timestable = (number * counter);
                TimeTableList[counter] = timestable;
                counter = (counter + 1);              
            }

            Console.WriteLine("The times tables for " + number + " are:");
            TimeTableList.ToList().ForEach(i => Console.WriteLine(i.ToString()));
            Console.Read();
        }
    }
}
如果您能帮我解决这个问题,我们将不胜感激

谢谢

变化

while (counter <= 10) 

循环从0开始,在10索引处结束,即11个元素

 while (counter <= 9)


您正在尝试访问数组索引11。数组索引从0开始。因此,您需要从条件中减去1或删除条件中的=计数器必须小于10,而不是小于等于。数组从0开始计数。然后您的元素从0到9被索引。

这两个都很好。谢谢数据有-1而没有理由或解释是很好的。您的表大小是10,但存储空间最多是11。0-10,因为
while (counter < 10) 
 while (counter <= 9)
 while (counter < 10)