C# foreach工作时未正确编制索引

C# foreach工作时未正确编制索引,c#,for-loop,indexing,C#,For Loop,Indexing,我已经使用这个类的数组有一段时间了,我也一直在使用foreach,但是当我尝试使用for循环(实际上是索引)访问它时,它就退出了数组。有什么帮助吗 -工作- [TestMethod] public void Scorer_FullHouse_ReturningZero() { SetOfDice[] diceRolls = new SetOfDice[] { new SetOfDice(new int[] {1,1,2,2,3}), new SetOfDi

我已经使用这个类的数组有一段时间了,我也一直在使用foreach,但是当我尝试使用for循环(实际上是索引)访问它时,它就退出了数组。有什么帮助吗

-工作-

[TestMethod]
public void Scorer_FullHouse_ReturningZero()
{
    SetOfDice[] diceRolls = new SetOfDice[] {
        new SetOfDice(new int[] {1,1,2,2,3}),
        new SetOfDice(new int[] {3,5,3,4,3}),
        new SetOfDice(new int[] {7,7,7,7,7})
    };

    foreach (SetOfDice roll in diceRolls)
    {
        int score = scorer.getScore(roll, category);
            Assert.AreEqual(score, 0);
    }
}
-不起作用-

System.IndexOutOfRangeException:索引超出了数组的边界

[TestMethod]
公开无效记分员
{
SetOfDice[]骰子卷=新的SetOfDice[]{
新的数据集(新的int[]{1,2,1,4,1}),
新的数据集(新的int[]{1,7,6,5,2}),
新的数据集(新的int[]{8,8,8,8})
};
int[]expectedResults=新int[]{
9,
0,
40
};
for(int i=0;i
也许问题出在其他地方。但是您可以通过查看堆栈跟踪来确定它。您还可以按如下方式更改for循环:

for (int i = 0; i < Math.Min(expectedResults.Length,diceRolls.length) ; i++)
    {
        Assert.AreEqual(expectedResults[i], scorer.getScore(diceRolls[i], GameVariables.Category.ThreeOfAKind));
    }
for(int i=0;i

现在,如果您使用这个for循环执行它,并且您仍然拥有
System.IndexOutOfRangeException
,那么您知道它来自其他方面

检查堆栈跟踪-您确定错误不是来自scorer.getScore
?旁注:我不确定游戏是什么,但在Yahtzee,5种类型可以被视为“满屋子”,它是在别处发现的!谢谢
for (int i = 0; i < Math.Min(expectedResults.Length,diceRolls.length) ; i++)
    {
        Assert.AreEqual(expectedResults[i], scorer.getScore(diceRolls[i], GameVariables.Category.ThreeOfAKind));
    }