【发布时间】:2021-05-08 08:20:32
【问题描述】:
我在下面编写了代码,我使用Id 填写名称。
namespace ConsoleApp1
{
class test
{
public int Id { get; set; }
public string Name { get { return Name; } set { Name = Id.ToString(); } }
}
class Program
{
static void Main(string[] args)
{
List<test> t1 = new List<test>();
t1.Add(new test() { Id = 1 });
t1.Add(new test() { Id = 2 });
t1.Add(new test() { Id = 3 });
t1.Add(new test() { Id = 4 });
t1.Add(new test() { Id = 5 });
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(t1));
}
}
}
代码运行时,抛出异常:
抛出了“System.StackOverflowException”类型的异常`。
如何解决问题?
【问题讨论】: