为什么我的数组初始化代码会导致抛出StackOverflowException?

MCS*_*MCS 1 c# stack-overflow arrays

我的类构造函数中的以下代码行抛出了StackOverflowException:

myList = new string[]{};  // myList is a property of type string[]
Run Code Online (Sandbox Code Playgroud)

为什么会这样?什么是初始化空数组的正确方法?


更新:原因在于setter,我试图修剪所有值:

set 
{
  for (int i = 0; i < myList.Length; i++)
     {
        if (myList[i] != null) myList[i] = myList[i].Trim();
     }
}
Run Code Online (Sandbox Code Playgroud)

Jon*_*øgh 8

如果myList是一个属性,你是否检查了它的setter的主体不会递归地分配给它自己而不是支持字段,如:

private string[] _myList;

public string[] myList { 
  get { 
    return _myList; 
  }
  set { 
    _myList = value;
  }
Run Code Online (Sandbox Code Playgroud)

}