Why does my array initialization code cause a StackOverflowException to be thrown?
- by MCS
The following line of code in my class constructor is throwing a StackOverflowException:
myList = new string[]{}; // myList is a property of type string[]
Why is that happening? And what's the proper way to initialize an empty array?
UPDATE: The cause was in the set method, in which I was attempting to trim all values. What's wrong with this code:
set
{
for (int i = 0; i < myList.Length; i++)
{
if (myList[i] != null) myList[i] = myList[i].Trim();
}
}