Adding events via OnClick, getting back argument out of range exception

So I have this code block.

    private void PopulateAnswerBoxItems(int charID, EventPackage packet)
    {
        for(int i = 0; i< packet.listOfAnswers.Count;i++)
        {
            GameObject answer = Instantiate(answerItem);
            answer.GetComponent<UI_EventResolutionPackage>().AssignData(packet.listOfAnswers*);*

Button attached = answer.GetComponentInChildren();
attached.onClick.AddListener(() => PopulateFinalDescriptors(charID, packet.listOfAnswers*));*
attached.onClick.AddListener(() => SetActiveScreen(1));

listOfAnswers.Enqueue(answer);

answer.transform.SetParent(answersBox.transform);
answer.transform.localScale = Vector3.one;
}
}
When I click on the end result (a button), it throws an ArgumentOutOfRange Exception at the first onClick.AddListener line.
the charID is valid, the button I’m clicking on is valid, the EventPackage is valid (not null), the list of answers is valid (all not null)…I don’t know why it’s doing this.
I’m not really looking for a solution per se, I just want to know where I should look next to solve this issue.
This is the error message.
[79506-question.png|79506]_
_

This is because of the way closures work in C#.

When you write attached.onClick.AddListener(() => PopulateFinalDescriptors(charID, packet.listOfAnswers_));, you are creating a closure : () => PopulateFinalDescriptors(charID, packet.listOfAnswers*).*_
Variables in a closure are not evaluated until the closure is executed. In this case, the problem is the ivariable. By the time your closure is executed, your loop is over, and i equals packet.listOfAnswers (that is true for all of your closures). Hence the argument out of range exception.
To prevent this, you can use another variable, defined in the loop.
int j = i;
attached.onClick.AddListener(() => PopulateFinalDescriptors(charID, packet.listOfAnswers[j]));
That way you force j to evaluate to the current value of i, and the correct value will be used.