【问题标题】:Getting an unexpected 'System.StackOverflowException' in C# code在 C# 代码中出现意外的“System.StackOverflowException”
【发布时间】:2013-02-25 19:17:01
【问题描述】:

我有以下代码,由于某种原因,我收到了错误:

“System.StackOverflowException”类型的未处理异常 发生在 WpfApplication1.exeat the line :this.JointName = 联合;inside thepublic Customer(字符串联合,字符串动作)

当我运行代码时。

Class1.cs 代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WpfApplication1
{
    public class Customer
    {
        public String JointName { get; set; }
        public String ActionName { get; set; }
        public List<Customer> lst = new List<Customer>();

        public Customer(String joint, String action)
        {
            this.JointName = joint;
            this.ActionName = action;
            this.lst.Add(new Customer(this.JointName, this.ActionName));
        }

        public List<Customer> GetList()
        {
            return this.lst;
        }
    }
}

MainWindow.xaml.cs 代码:

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    /// 

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Customer Data1 = new Customer("Joint1", "Action1");
            List<Customer> List = Data1.GetList();
            dataGrid1.ItemsSource = List;

        }
    }
}

你们能看到我在这里做错了什么吗?我不知道我在哪里可以有一个无限循环..

【问题讨论】:

  • 基本上,当您创建客户时,它会在客户的内部列表中添加一个新客户,这会在其内部列表中添加一个新客户,依此类推......
  • 你反复初始化 Cosntructor 无限次

标签: c#


【解决方案1】:

Class1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WpfApplication1
{
    public class Customer
    {
        public String JointName { get; set; }
        public String ActionName { get; set; }


        public Customer(String joint, String action)
        {
            this.JointName = joint;
            this.ActionName = action;

        }


    }
}

MainWindow.xaml.cs 代码:

 namespace WpfApplication1
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        /// 

        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                Customer Data1 = new Customer("Joint1", "Action1");
                List<Customer> list = new List<Customer>();
                list.add(Data1);
                dataGrid1.ItemsSource = list;

            }
        }
    }

【讨论】:

    【解决方案2】:

    您反复递归地调用Customer 构造函数。所以你的调用堆栈看起来像这样:

    Customer(String joint, String action)
    Customer(String joint, String action)
    Customer(String joint, String action)
    Customer(String joint, String action)
    Customer(String joint, String action)
    ...
    

    每个方法在完成后等待调用list.Add

    碰巧对JointNameset 属性的调用是破坏堆栈帧的最后一个方法调用。

    【讨论】:

      【解决方案3】:

      Customer 的构造函数在最后一行包含对自身的调用:

      this.lst.Add(new Customer(this.JointName, this.ActionName))

      这就是我们错误的根源。

      【讨论】:

      • 扩展您的答案。说明为什么会导致错误等以及应该是什么。
      【解决方案4】:

      Customer 的构造函数总是执行“new Customer”,然后调用 Customer 的构造函数,以此类推,无止境。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-11-14
        • 1970-01-01
        • 2017-04-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-04
        • 1970-01-01
        相关资源
        最近更新 更多