c# - 系统.StackOverflowException

标签 c# asp.net .net stack-overflow c#-2.0

请帮助我解决 System.StackOverflowException 我想用 .aspx 将记录写入数据库我使用 4 层架构来实现这一切都正常但是当我编译页面然后它显示要插入数据的字段时,当我将数据插入这些字段并单击提交按钮然后显示 System.StackOverflowException 发生

public class Customers
{
    public Customers()
    {
        int CustomerID = 0;
        string Fname = string.Empty;
        string Lname = string.Empty;
        string Country = string.Empty;
    }
    public int CustomerID
    {
        get { return CustomerID; }
        set { CustomerID = value; }
    }
    public string Fname
    {
        get { return Fname; }
        set { Fname = value; }****
    }
    public string Lname
    {
        get { return Lname; }
        set { Lname = value; }
    }
    public string Country
    {
        get { return Country; }
        set { Country = value; }
    }

当页面执行时弹出一个窗口并显示 System.StackOverflowException occurred 请给我解决这个问题的方法

最佳答案

public int CustomerID
{
    get { return CustomerID; }
    set { CustomerID = value; }
}

您正在递归地将值分配给自身。其他属性也一样。

您需要用另一个名称定义一个备份字段,例如:

private int _CustomerId;
public int CustomerID
{
    get { return _CustomerID; }
    set { _CustomerID = value; }
}

或者更好:

public int CustomerId {get; set;}

关于c# - 系统.StackOverflowException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1672547/

相关文章:

c# - 替代嵌套类型的类型 Expression<Func<T>>

c# - Process.Kill() 问题

c# - 序列化 View 模型时出错 : "Type ' System. Web.HttpPostedFileWrapper' 无法序列化。”

asp.net - 在 vb.net 中通过反射抛出类型 'E' 的异常

.net - 依赖注入(inject)启动性能

c# - 如何测量特定逻辑线程的 CPU 使用率?

c# - 单元测试模型 - 建议

c# - 有人可以帮我将多个字符串数组分配给一个二维字符串数组吗?

jquery - 在 Jquery 中调用 WCF 服务抛出错误

c# - 在 asp.net mvc 应用程序中从 Javascript 访问 C# 变量