I wrote code below where I use the Id
to fill in the name.
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));
}
}
}
When the code runs, an exception is raised:
Exception of type 'System.StackOverflowException' was thrown`.
How resolve problem?