简体   繁体   中英

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. 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.

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:

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. Maybe the problem is related to the declaration and initialization of exerciseAnswers and the foreach loop.

Has somebody have an idea?

Thank you!

I believe you are using Append method from System.Linq namespace

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

This method returns a new IEnumerable which contains your 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. 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); exerciseAnswers[elementIndex] = exerciseAnswer;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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