为什么此方法调用 属性 会导致堆栈溢出?
Why is this method call on a property causing a stack overflow?
我正在用 C# 编写一个冒泡排序,运行 解决了这个问题。下面是我的 类,在 类 之后我会描述我的问题。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BubbleSort
{
class Person
{
public String FirstName { get; set; }
public String LastName {get; set;}
public String PhoneNumber { get; set; }
public Person(String firstName, String lastName)
{
FirstName = firstName;
LastName = lastName;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Collections;
namespace BubbleSort
{
class ListProvider
{
//Class Variables
private StreamReader fileReader;
//private List<Person> test;
#region Properties
public List<Person> TheList
{
get { return TheList; }
set { TheList = value; }
}
#endregion
/// <summary>
/// I wish C# had a Scanner class :/. How different are these languages really?
/// </summary>
/// <param name="fileName"></param>
public ListProvider(string fileName)
{
// test = new List<Person>();
using (fileReader = new StreamReader(fileName))
{
String line = "";
while (fileReader.Peek() != -1)
{
line = fileReader.ReadLine();
String[] nameArray = line.Split(' ');
TheList.Add(new Person(nameArray[0], nameArray[1]));
//test.Add(new Person(nameArray[0], nameArray[1]));
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BubbleSort
{
class Program
{
static void Main(string[] args)
{
ListProvider listProvider = new ListProvider("C:\Users\JC Boss\Desktop\Career\Programming Practice\BubbleSort\BubbleSort\names.txt");
foreach (Person person in listProvider.TheList)
{
Console.WriteLine(person.FirstName + " " + person.LastName);
}
}
好的。因此,当我尝试向该列表中添加一个新的 Person 时,我得到了一个堆栈溢出异常。现在,如果我要取消注释 List 变量测试,并添加到它。它会工作正常,并且不会抛出任何错误。为什么会这样?我环顾四周,但不明白为什么 属性 会发生这种情况?
}
}
public List<Person> TheList
{
get { return TheList; }
set { TheList = value; }
}
这个setter指的是它自己。任何读取或写入它的尝试都会导致无限递归。
我正在用 C# 编写一个冒泡排序,运行 解决了这个问题。下面是我的 类,在 类 之后我会描述我的问题。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BubbleSort
{
class Person
{
public String FirstName { get; set; }
public String LastName {get; set;}
public String PhoneNumber { get; set; }
public Person(String firstName, String lastName)
{
FirstName = firstName;
LastName = lastName;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Collections;
namespace BubbleSort
{
class ListProvider
{
//Class Variables
private StreamReader fileReader;
//private List<Person> test;
#region Properties
public List<Person> TheList
{
get { return TheList; }
set { TheList = value; }
}
#endregion
/// <summary>
/// I wish C# had a Scanner class :/. How different are these languages really?
/// </summary>
/// <param name="fileName"></param>
public ListProvider(string fileName)
{
// test = new List<Person>();
using (fileReader = new StreamReader(fileName))
{
String line = "";
while (fileReader.Peek() != -1)
{
line = fileReader.ReadLine();
String[] nameArray = line.Split(' ');
TheList.Add(new Person(nameArray[0], nameArray[1]));
//test.Add(new Person(nameArray[0], nameArray[1]));
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BubbleSort
{
class Program
{
static void Main(string[] args)
{
ListProvider listProvider = new ListProvider("C:\Users\JC Boss\Desktop\Career\Programming Practice\BubbleSort\BubbleSort\names.txt");
foreach (Person person in listProvider.TheList)
{
Console.WriteLine(person.FirstName + " " + person.LastName);
}
}
好的。因此,当我尝试向该列表中添加一个新的 Person 时,我得到了一个堆栈溢出异常。现在,如果我要取消注释 List 变量测试,并添加到它。它会工作正常,并且不会抛出任何错误。为什么会这样?我环顾四周,但不明白为什么 属性 会发生这种情况? } }
public List<Person> TheList
{
get { return TheList; }
set { TheList = value; }
}
这个setter指的是它自己。任何读取或写入它的尝试都会导致无限递归。