C# 4.0 在列表中添加类属性,在循环中添加视图
我有三个属性的“Product”类。它是一个简单的控制台应用程序,用户在其中给出三条记录,并将其放入列表中。我从类产品创建列表,但由于某些原因,它将成为无限条目!我不知道我做错了什么C# 4.0 在列表中添加类属性,在循环中添加视图,c#-4.0,C# 4.0,我有三个属性的“Product”类。它是一个简单的控制台应用程序,用户在其中给出三条记录,并将其放入列表中。我从类产品创建列表,但由于某些原因,它将成为无限条目!我不知道我做错了什么 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication5 { class P
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
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间控制台应用程序5
{
班级计划
{
静态void Main(字符串[]参数)
{
产品obj1=新产品();
}
}
类产品
{
公共int ID{get;set;}
公共字符串名称{get;set;}
公共字符串Price{get;set;}
公共产品()
{
Console.WriteLine(“输入产品名称:”);
字符串名称=Console.ReadLine();
Console.WriteLine(“输入产品价格:”);
字符串price=Console.ReadLine();
List MyList=新列表();
添加(新产品(){ID=1,Name=Name,Price=Price});
foreach(MyList中的var项)
{
Console.WriteLine(“ID”+item.ID+“Name”+item.Name);
}
}//最终产品类别
}
在构造器中创建一个新产品,在构造器中创建一个新产品,在产品构造器中创建一个新产品,这将调用产品构造器,从而创建另一个产品。
换言之,这是一个无限循环!在产品的构造函数中,您正在创建一个新产品,该新产品将依次调用产品的构造函数,该构造函数将创建另一个产品。
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)
{
List<Product> MyList = new List<Product>();
Console.WriteLine("Enter Product Name: ");
string name = Console.ReadLine();
Console.WriteLine("Enter Product Price: ");
string price = Console.ReadLine();
MyList.Add(new Product{ ID = 1, Name = name, Price = price });
foreach(var item in MyList)
{
Console.WriteLine("ID "+item.ID + " Name "+item.Name);
}
Console.WriteLine("End of Program; Press Enter to Exit");
Console.ReadLine();
}
}
public class Product
{
public int ID { get; set; }
public string Name { get; set; }
public string Price { get; set; }
public Product()
{
}
} //end product class
}
换句话说,这是一个无限循环!使用系统;
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)
{
List<Product> MyList = new List<Product>();
Console.WriteLine("Enter Product Name: ");
string name = Console.ReadLine();
Console.WriteLine("Enter Product Price: ");
string price = Console.ReadLine();
MyList.Add(new Product{ ID = 1, Name = name, Price = price });
foreach(var item in MyList)
{
Console.WriteLine("ID "+item.ID + " Name "+item.Name);
}
Console.WriteLine("End of Program; Press Enter to Exit");
Console.ReadLine();
}
}
public class Product
{
public int ID { get; set; }
public string Name { get; set; }
public string Price { get; set; }
public Product()
{
}
} //end product class
}
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间控制台应用程序5
{
班级计划
{
静态void Main(字符串[]参数)
{
List MyList=新列表();
Console.WriteLine(“输入产品名称:”);
字符串名称=Console.ReadLine();
Console.WriteLine(“输入产品价格:”);
字符串price=Console.ReadLine();
Add(新产品{ID=1,Name=Name,Price=Price});
foreach(MyList中的var项)
{
Console.WriteLine(“ID”+item.ID+“Name”+item.Name);
}
Console.WriteLine(“程序结束;按Enter键退出”);
Console.ReadLine();
}
}
公共类产品
{
公共int ID{get;set;}
公共字符串名称{get;set;}
公共字符串Price{get;set;}
公共产品()
{
}
}//最终产品类别
}
在数据类中通常没有UI交互
请允许我建议您通读一遍,这是一本学习如何首次编写C语言程序的好书。使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间控制台应用程序5
{
班级计划
{
静态void Main(字符串[]参数)
{
List MyList=新列表();
Console.WriteLine(“输入产品名称:”);
字符串名称=Console.ReadLine();
Console.WriteLine(“输入产品价格:”);
字符串price=Console.ReadLine();
Add(新产品{ID=1,Name=Name,Price=Price});
foreach(MyList中的var项)
{
Console.WriteLine(“ID”+item.ID+“Name”+item.Name);
}
Console.WriteLine(“程序结束;按Enter键退出”);
Console.ReadLine();
}
}
公共类产品
{
公共int ID{get;set;}
公共字符串名称{get;set;}
公共字符串Price{get;set;}
公共产品()
{
}
}//最终产品类别
}
在数据类中通常没有UI交互
我建议您完成,这是一本学习如何第一次编写C#程序的好书。不要从同一个构造函数中调用构造函数。您应该从该构造函数中删除所有内容,并提供一个包含名称和价格的第二个构造函数。然后您可以使用这些参数初始化属性。我认为从控制台读取在构造函数中,这是非常糟糕的设计,,,我第一次看到这样的东西:)不要从同一个构造函数中调用构造函数。你应该从这个构造函数中删除所有内容,并提供第二个包含名称和价格的构造函数。然后你可以用这些参数初始化属性。我认为从构造函数中的控制台读取是非常糟糕的符号,,,我第一次看到这样的东西:)它将是程序类的一部分。它将是程序类的一部分。