【问题标题】:set is throwing StackOverflowException C#设置抛出 StackOverflowException C#
【发布时间】:2015-03-25 13:17:56
【问题描述】:

我正在尝试获取此对象的列表并将其发送到文本文件。文本文件格式如下。

姓名、身份证号、部门、值

这有好几行,所以我用了一个 for 来读取它们。 这是读写文件的代码。

    public List<Employee> ReadFile(string fileName) {
        StreamReader fileIn = new StreamReader(fileName);
        fileIn = File.OpenText(fileName);
        List<Employee> list = new List<Employee>();
        string[] test;
        string name;
        string ID;
        string dep;
        string post;

        while (!fileIn.EndOfStream || !File.Exists(fileName)) {

            string inString = fileIn.ReadLine();
            test = inString.Split('#');
            name = test[0];
            ID = test[1];
            dep = test[2];
            post = test[3];
            Employee newEmp = new Employee(name, ID, dep, post);
            list.Add(newEmp);

        }
        fileIn.Close();
        return list;
    }

    public void WriteFile(List<Employee> outList, string file) {

        StreamWriter writeOut = new StreamWriter(file);

        for (int i = 0; i < outList.Count; i++) {

            writeOut.WriteLine(outList[i].name + '#' + outList[i].IDnum + '#' + outList[i].department + '#' + outList[i].position);

        }
        writeOut.close();
    }

这是我班级的代码。错误正在被抛出。

public class Employee {

    public string name { get { return name; } set { name = value; } }
    public string IDnum { get { return IDnum; } set { IDnum = value; } }
    public string department { get { return department; } set { department = value; } }
    public string position { get { return position; } set { position = value; } }

    public Employee() {

        name = string.Empty;
        IDnum = string.Empty;
        department = string.Empty;
        position = string.Empty;

    }

    public Employee(string newName, string newID) {

        name = newName;
        IDnum = newID;
        department = string.Empty;
        position = string.Empty;

    }

    public Employee(string newName, string newID, string newDep, string
    newPost) {

        name = newName;
        IDnum = newID;
        department = newPost;
        position = newDep;

    }

}

我不确定是否缺少某种格式,以便 set 函数根据需要运行。这是我为文件的输入和输出调用的函数。我相信它永远不会成功,所以这很可能是我导入数据的方式。

【问题讨论】:

    标签: c#


    【解决方案1】:

    你应该改变

    public string name { get { return name; } set { name = value; } }
    public string IDnum { get { return IDnum; } set { IDnum = value; } }
    public string department { get { return department; } set { department = value; } }
    public string position { get { return position; } set { position = value; } }
    

    public string name { get; set; }
    public string IDnum { get; set; }
    public string department { get; set; }
    public string position { get; set; }
    

    或引入支持字段:

    private string _name;
    public string name { get { return _name; } set { _name = value; } }
    

    有关 C# 中自动实现的属性的更多信息,请参阅 https://msdn.microsoft.com/en-us/library/bb384054.aspx

    请注意,公共属性的常用命名是 PascalCasing。您在 PascalCasing 中的属性如下所示:

    public string Name { get; set; }
    public string IdNum { get; set; }
    public string Department { get; set; }
    public string Position { get; set; }
    

    【讨论】:

      【解决方案2】:

      这是一个非常常见的问题... C# 成人礼!

      让我们看一下单个属性(但这适用于您的所有属性)...

      public string name { get { return name; } set { name = value; } }
      

      那么当您尝试myObj.name = "foo"; 时会发生什么?

      set 方法中,您引用了相同的属性name。所以它会尝试访问name,它会再次循环(一次又一次,递归直到你 StackOverflow)。

      具有适当命名约定的支持字段是这里的规范:

      private string name;
      public string Name{get { return name; } set{ name = value; }}
      

      或者更好,如果不涉及逻辑,auto-property

      public string Name{ get; set; }
      

      【讨论】:

        【解决方案3】:

        你不断地递归调用 IDnum 和其他属性,直到堆栈溢出

        public string IDnum { get { return IDnum; } set { IDnum = value; } }
        

        当你做类似的事情时

        IDnum = someValue;
        

        调用IDnum 的setter,运行setter 中的代码

        IDnum = 值

        依次调用 IDnum 的设置器,直到你用完堆栈。

        修复

        在您的情况下,您似乎可以使用自动属性

        public string IDnum { get; set; }
        

        【讨论】:

        • 那么,语法应该如何?应该只是get {return IDnum;set{ IDnum = value;
        • ++,其他属性也有同样的问题。
        • 非常感谢!我之前在使用自动时遇到了问题,但看起来我纠正了导致该问题的错误。
        • 很高兴你解决了。如果您不想使用自动属性,则必须设置一个变量来存储属性值(称为“后备存储”,使用不同的名称,例如 private string idnum;
        猜你喜欢
        • 1970-01-01
        • 2021-01-25
        • 1970-01-01
        • 1970-01-01
        • 2011-05-18
        • 1970-01-01
        • 2021-03-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多