List<element> initialization fires “Process is terminated due to StackOverflowException”

被刻印的时光 ゝ 提交于 2019-12-08 05:38:27

问题


I have structs like below and when I do that initialization:

ArrayList nodesMatrix = null;
List<vertex> vertexMatrix = null;
List<bool> odwiedzone = null;
List<element> priorityQueue = null;

vertexMatrix = new List<vertex>(nodesNr + 1);
nodesMatrix = new ArrayList(nodesNr + 1);
odwiedzone = new List<bool>(nodesNr + 1);
priorityQueue = new List<element>();

arr.NodesMatrix = nodesMatrix;
arr.VertexMatrix = vertexMatrix;
arr.Odwiedzone = odwiedzone;
arr.PriorityQueue = priorityQueue; //only here i have exception

debuger fires Process is terminated due to StackOverflowException :/ Some idea why this collection fires this exception ?

private struct arrays
    {
        ArrayList nodesMatrix;

        public ArrayList NodesMatrix
        {
            get { return nodesMatrix; }
            set { nodesMatrix = value; }
        }
        List<vertex> vertexMatrix;

        public List<vertex> VertexMatrix
        {
            get { return vertexMatrix; }
            set { vertexMatrix = value; }
        }
        List<bool> odwiedzone;

        public List<bool> Odwiedzone
        {
            get { return odwiedzone; }
            set { odwiedzone = value; }
        }
        public List<element> PriorityQueue
        {
            get { return PriorityQueue; }
            set { PriorityQueue = value; }
        }


    }
    public struct element : IComparable
    {
        public double priority
        {
            get { return priority; }
            set { priority = value; }
        }
        public int node
        {
            get { return node; }
            set { node = value; }
        }
        public element(double _prio, int _node)
        {
            priority = _prio;
            node = _node;
        }

        #region IComparable Members

        public int CompareTo(object obj)
        {
            element elem = (element)obj;
            return priority.CompareTo(elem.priority);
        }

        #endregion

回答1:


Your PriorityQueue property is referencing itself.
You need to change the accessors to use a field.

List<element> priorityQueue;
public List<element> PriorityQueue
{
    get { return priorityQueue; }
    set { priorityQueue = value; }
}

However, you should use automatically-implemented properties instead:

public List<element> PriorityQueue { get; set; }



回答2:


Your property setter is recursive.

    public List<element> PriorityQueue
    {
        get { return PriorityQueue; }
        set { PriorityQueue = value; }
    }

Change this to be:

    public List<element> PriorityQueue
    {
        get { return priorityQueue; }
        set { priorityQueue = value; }
    }


来源:https://stackoverflow.com/questions/2511240/listelement-initialization-fires-process-is-terminated-due-to-stackoverflowex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!