我有具有三个属性的“产品”类。它是一个简单的控制台应用程序,用户在其中提供了三条记录并将其放入列表中。我从类产品创建列表,但由于某种原因它会无限进入!我不知道我做错了什么
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
Product obj1 = new Product();
}
}
class Product
{
public int ID { get; set; }
public string Name { get; set; }
public string Price { get; set; }
public Product()
{
Console.WriteLine("Enter Product Name: ");
string name = Console.ReadLine();
Console.WriteLine("Enter Product Price: ");
string price = Console.ReadLine();
List<Product> MyList = new List<Product>();
MyList.Add(new Product() { ID = 1, Name = name, Price = price });
foreach(var item in MyList)
{
Console.WriteLine("ID "+item.ID + " Name "+item.Name);
}
} //end product class
}