Your problem is with this line:
currentPossibilities = allFiveLetterWords;
A list is allocated on the heap when you call
new List<string>()
and the address of that list is stored in the variable. When you access the variable (ie. calling
.Add()
) you're asking C# to find the list in the heap, then call the
Add()
method.
With the above, what you've done is said "the
currentPossibilities
variable should point to the same list as
allFiveLetterWords
". Then, in your text changed event you call:
currentPossibilities.Clear();
Which also says "clear all values out of the list at the address of
currentPosibilities
", which also happens to be the same address as
allFiveLetterWords
. You can fix this easily by simply creating a new list:
currentPossibilities = new List<string>(allFiveLetterWords);
This page has a fairly decent explanation of heap vs. stack[
^]