简体   繁体   中英

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.

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.

You are instantiating a List within the constructor of your Customers class. 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.

A good rule of thumb, if you get a stack overflow in C#, look for recursion that never terminates.

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().....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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