简体   繁体   English

堆栈溢出异常 - 为什么?

[英]Stack overflow exception—why?

I keep getting a stack overflow exception with this. 我不断得到一个堆栈溢出异常。 I've narrowed it down to this class to try and figure out what was wrong but I simply have no idea why I keep getting this error message? 我已经把它缩小到这个类来试图弄清楚出了什么问题,但我根本不知道为什么我一直收到这个错误信息? Originally I have the user interface in another class but just to eliminate everything else like problems in my calling methods I moved the essentials to this class to try and figure out what was wrong. 最初我在另一个类中有用户界面,但只是为了消除其他一切,比如我的调用方法中的问题,我将基本要素移到这个类中,试图弄清楚出了什么问题。 I thought it might be my properties? 我以为这可能是我的财产? Maybe it's obvious to everyone else but I simply don´t understand. 也许对其他人来说很明显,但我根本不明白。

Since I am very new to programming I would appreciate some help on what I have done wrong. 由于我对编程很陌生,所以我会很感激我对错误的帮助。 From what I understand this problem occurs when you have something like an infinite loop? 根据我的理解,当你有无限循环之类的东西时会出现这个问题?

namespace MyNameSpace
{
    public class Customers
    {
        private List<Customers> customers; 

        public Customers()
        {
            customers = new List<Customers>();

            AddCustomer(new Customers() 
            { 
            Name = "A", Telephone="1" 
            });
        }

        public string Name
        {
            get;
            set;
        }
        public string Telephone
        {
            get;
            set;
        }

        public void RunTest()
        {

            Console.WriteLine();
            Console.WriteLine("****** VIDEOSTORE ******");
            Console.WriteLine();
            Console.WriteLine("1. Show Customers");
            Console.WriteLine("6. Quit");

            string userChoice = Console.ReadLine();

            switch (userChoice)
            {
                case "1": 
                    View();
                    break;         

                    break;
                case "2":
                    break;
            }
        }

        public void View()
        {
            foreach (Customers c in customers)
            {
                Console.WriteLine();
                Console.WriteLine(c.Name);
                Console.WriteLine(c.Telephone);
                Console.WriteLine();
            }
        }

        public void AddCustomer(Customers custom)                           
        {
            customers.Add(custom);          
        }
    }
}

In the Customers constructor you calling the Customers constructor again, creating infinite recursion. 在Customers构造函数中,再次调用Customers构造函数,创建无限递归。

You should have a separate class for a list of Customers and for a single Customer: 您应该为客户列表和单个客户提供单独的类:

namespace MyNameSpace
{
    public class Customer
    {
        public string Name
        {
            get;
            set;
        }
        public string Telephone
        {
            get;
            set;
        }
    }

    public class Customers
    {
        private List<Customer> customers; 

        public Customers()
        {
            customers = new List<Customer>();

            AddCustomer(new Customer() 
            { 
            Name = "A", Telephone="1" 
            });
        }


        public void RunTest()
        {

            Console.WriteLine();
            Console.WriteLine("****** VIDEOSTORE ******");
            Console.WriteLine();
            Console.WriteLine("1. Show Customers");
            Console.WriteLine("6. Quit");

            string userChoice = Console.ReadLine();

            switch (userChoice)
            {
                case "1": 
                    View();
                    break;         

                    break;
                case "2":
                    break;
            }
        }

        public void View()
        {
            foreach (Customer c in customers)
            {
                Console.WriteLine();
                Console.WriteLine(c.Name);
                Console.WriteLine(c.Telephone);
                Console.WriteLine();
            }
        }

        public void AddCustomer(Customer customer)                           
        {
            customers.Add(customer);          
        }
    }
}

您正在调用在Customers构造函数中创建新的Customers对象。

Your constructor for Customers calls itself, causing an endless loop. 客户的构造函数调用自身,导致无限循环。

public Customers()
{
    customers = new List<Customers>();

    AddCustomer(new Customers() // <- Here
    { 
    Name = "A", Telephone="1" 
    });
}

Endless recursive calls to a function will cause a StackOverFlow. 对函数的无休止递归调用将导致StackOverFlow。

You are instantiating a List within the constructor of your Customers class. 您正在Customers类的构造函数中实例化List。 This will cause an infinite loop and result in a stack overflow. 这将导致无限循环并导致堆栈溢出。

I think you should try to separate your code into multiple classes. 我认为你应该尝试将你的代码分成多个类。

public class Customer
{
    public string Name { get; set; }
    public string Telephone { get; set; }
}

public class Program
{
    private List<Customer> _customers = new List<Customer();

    public Program()
    {
        _customers.Add(new Customer() 
        { 
            Name = "A", Telephone="1" 
        });
    }

    // your other methods here - like View()
}

Your constructor is calling itself (new Customers()), which causes it to never return. 您的构造函数正在调用它自己(new Customers()),这会导致它永远不会返回。

A good rule of thumb, if you get a stack overflow in C#, look for recursion that never terminates. 一个好的经验法则,如果你在C#中获得堆栈溢出,请寻找永不终止的递归。

stack overflow errors are usually not from endless loops, but from endless recursion (well, not really endless in practice, it continues calling itself until the stack is full and then the exception is thrown). 堆栈溢出错误通常不是来自无限循环,而是来自无休止的递归(好吧,在实践中并不是真的无穷无尽,它会继续调用自己,直到堆栈已满,然后抛出异常)。

If you have a method that uses recursion (ie it calls itself) you have to make sure that this only happens a limited number of times. 如果你有一个使用递归的方法(即它自己调用),你必须确保这只发生有限次数。 If you don't do that, you get a method that calls itself with a method call that calls itself with a method call that calls itself with a method call that calls itself (continue this many many times, until the stack is full).... Like Customers() calling Customers() calling Customers() calling Customers() calling Customers() calling Customers() calling Customers() calling Customers()..... 如果你不这样做,你会得到一个方法,它通过一个方法调用来调用自身,该方法调用通过一个调用自身的方法调用来调用自身(继续这么多次,直到堆栈已满)。 ...像Customers()调用Customers()调用Customers()调用Customers()调用Customers()调用Customers()调用Customers()调用Customers().....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM