Why does my array initialization code cause a StackOverflowException to be thrown?

Posted by MCS on Stack Overflow See other posts from Stack Overflow or by MCS
Published on 2010-05-26T17:11:10Z Indexed on 2010/05/26 17:21 UTC
Read the original article Hit count: 224

Filed under:
|
|

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();
     }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about arrays