简体   繁体   English

在 C# 中的 foreach 循环后无法将对象添加到数组中

[英]Can't add objects to an array after a foreach loop in C#

I can't append an object to an array after a foreach loop.在 foreach 循环之后,我无法将对象附加到数组中。 The object is okay, it contains all the right values which I found out through debugging.该对象没问题,它包含我通过调试发现的所有正确值。

In the end I want to have a custom Exercise object which contains a user-chosen number of custom ExerciseAnswer objects also.最后,我想要一个自定义的练习对象,其中还包含用户选择的自定义练习答案对象数量。 The array of ExerciseAnswer objects is the problem. ExerciseAnswer 对象的数组是问题所在。

Here is the interesting part of my method:这是我的方法中有趣的部分:

static void CreateNewExerciseTest()
{
     string? exerciseName = "Test Exercise";
     string? exerciseTopic = "Test";
     string exerciseQuestion = "Does it work?";
     int numberOfAnswers = 2;
     int numberOfApplicableAnswers = 0;
     ExerciseAnswer[] exerciseAnswers = new ExerciseAnswer[2];                    

     foreach (ExerciseAnswer answer in exerciseAnswers)
     {
         int exerciseAnswerId = GenerateId();
         Console.WriteLine("\nEnter a name for this Exercise answer: ");
         string? exerciseAnswerName = Console.ReadLine();
         Console.WriteLine("Enter this answer for the Exercise: ");
         string exerciseAnswerContent = Console.ReadLine();
         Console.WriteLine("Enter y (yes) if this Exercise answer is applicable, 
                            otherwise press n (no) or any other key: ");
         char applicableAnswer = Console.ReadKey().KeyChar;
         bool applicable = ExerciseAnswer.EvaluateExerciseAnswer(applicableAnswer);
         if (applicable == true)
         {
             numberOfApplicableAnswers++;
         }

         ExerciseAnswer exerciseAnswer = new ExerciseAnswer(exerciseAnswerId,   
         exerciseAnswerName, exerciseAnswerContent, applicable);
         exerciseAnswers.Append(exerciseAnswer);
         // ... 
    }
}

This the GenerateId method:这是 GenerateId 方法:

static int GenerateId()
{
    return ++id;
}

The array exerciseAnswers does not contain the ExerciseAnswer elements it should while the exerciseAnswer object in the line above does.数组 exerciseAnswer 不包含它应该包含的 ExerciseAnswer 元素,而上面一行中的 exerciseAnswer 对象包含。 Maybe the problem is related to the declaration and initialization of exerciseAnswers and the foreach loop.可能问题与exerciseAnswers 和foreach 循环的声明和初始化有关。

Has somebody have an idea?有人有想法吗?

Thank you!谢谢!

I believe you are using Append method from System.Linq namespace我相信您正在使用System.Linq命名空间中的Append方法

public static IEnumerable<TSource> Append<TSource>(this IEnumerable<TSource> source, TSource element);

This method returns a new IEnumerable which contains your exerciceAnswer此方法返回一个新的IEnumerable ,其中包含您的exerciceAnswer

With this piece of code you can understand what is going on:通过这段代码,您可以了解发生了什么:

var result = exerciseAnswers.Append(exerciseAnswer);
Console.WriteLine($"exerciseAnswers count = {exerciseAnswers.Count()}");
Console.WriteLine($"result count = {result.Count()}");

Console output:控制台输出:

exerciseAnswers count = 2
result count = 3

Append will just append to existing elements of array, since in your case size is already defined as 2(new ExerciseAnswer[2]) so it is not appending anything. Append 只会追加到数组的现有元素,因为在您的情况下,大小已经定义为 2(new ExerciseAnswer[2]),因此它不会追加任何内容。 What you can do is either have a new array and get the elements added to it or just get the index of element you are running the loop and replace same in the array.您可以做的是拥有一个新数组并将元素添加到其中,或者只是获取您正在运行循环的元素的索引并在数组中替换相同的元素。 Something like below:- int elementIndex = Array.IndexOf(exerciseAnswers,answer);如下所示:- int elementIndex = Array.IndexOf(exerciseAnswers,answer); exerciseAnswers[elementIndex] = exerciseAnswer;练习答案[元素索引] = 练习答案;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM