有没有办法循环遍历数组,在 C# 中编辑数组的内容,然后再次循环以显示编辑后的内容?
我正在尝试遍历一系列产品。我想知道是否可以更改数组的元素,然后再次循环以将其与编辑后的元素一起显示。
下面是代码:
public enum ProductStatus
{
ForSale,
Sold,
Shipped
}
class Product
{
private string description;
private decimal price;
private ProductStatus status;
public string Description
{
get { return description; }
set { description = value; }
}
public decimal Price
{
get { return price; }
set { price = value; }
}
public ProductStatus Status
{
get { return status; }
set { status = 0; }
}
然后实例化数组:
Product[] products = new Product[3];
products[0] = new Product() { Description = "Dress", Price = 69.99M };
products[1] = new Product() { Description = "Hat", Price = 5.95M};
products[2] = new Product() { Description = "Slacks", Price = 32.00M};
重写了 ToString()
方法后,我像这样循环遍历它:
ProductStatus status = ProductStatus.ForSale;
ProductStatus nextStatus = status + 1;
for (int i = 0; i < products.Length; i++)
{
Product p = products[i];
Console.WriteLine("{0}: " + p.ToString(), status.ToString());
}
我想使用 nextStatus 将第一个索引的 ProductStatus 从 " 更改ForSale”到“Sold”(然后是“Shipped”),然后再次循环数组以显示更改。如何做到这一点?
I am trying to loop through an array of products. I was wondering if it would at all be possible to change an element of the array, and then loop through it again to display it with the edited element.
Here is the code:
public enum ProductStatus
{
ForSale,
Sold,
Shipped
}
class Product
{
private string description;
private decimal price;
private ProductStatus status;
public string Description
{
get { return description; }
set { description = value; }
}
public decimal Price
{
get { return price; }
set { price = value; }
}
public ProductStatus Status
{
get { return status; }
set { status = 0; }
}
And then to instantiate the array:
Product[] products = new Product[3];
products[0] = new Product() { Description = "Dress", Price = 69.99M };
products[1] = new Product() { Description = "Hat", Price = 5.95M};
products[2] = new Product() { Description = "Slacks", Price = 32.00M};
Having overridden the ToString()
method, I loop through it like so:
ProductStatus status = ProductStatus.ForSale;
ProductStatus nextStatus = status + 1;
for (int i = 0; i < products.Length; i++)
{
Product p = products[i];
Console.WriteLine("{0}: " + p.ToString(), status.ToString());
}
I'd like to use the nextStatus to change the first index's ProductStatus from "ForSale" to "Sold" (and then "Shipped" afterwards), and then loop through the array again to show the changes. How does one do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
虽然您的问题有点不清楚,但我想这就是您想要的(下面的代码未测试)。
输出可能如下所示(假设
ForSale
为初始状态):无论如何,您确定要申请吗这样做?在我看来,您想要实现的目标略有不同,如下所示:
在这种情况下,输出如下:
希望有帮助。
Although your question is a little unclear I suppose this is what you want (the code below is not tested).
And the output could appear as follows (assuming
ForSale
as initial status):Anyway, are you sure you want your application to behave this way? In my opinion what you are trying to achieve is slightly different, something like this:
In this case the output would be the following:
Hope it helps.
如果您的问题是“如何确定更改的项目并查看它们”,您可以轻松使用其结尾列表:
If your question is "How can I determine the changed items and view them", you can easily use a list of their endexes:
为什么不创建一个包含更改状态的新集合,例如:
Why not create a new collection containing the changed statuses, e.g.: