开发者

Best data structure to represent grocery items in C#?

开发者 https://www.devze.com 2022-12-24 21:49 出处:网络
What\'s the best data structure for a 2D array of size unknown x 2. Which means one dimension is dynamic (list of grocery ite开发者_开发知识库ms) and the other is fixed (price and quantity).Personally

What's the best data structure for a 2D array of size unknown x 2. Which means one dimension is dynamic (list of grocery ite开发者_开发知识库ms) and the other is fixed (price and quantity).


Personally, I would use a List<T>, with T being a custom class.

This would be something like:

public class GroceryItem { 
   public string Name { get; set; }
   // ...
}
public class PurchaseLineItem
{
    public double Price { get; set; }
    public int Quantity { get; set; }
    public GroceryItem Item { get; set; }
    // ...
}

Then just do:

List<PurchaseLineItem> items = new List<PurchaseLineItem>();

This gives you the flexibility to have your price and quanities, for any number of items. It's easy to add to this as needed, and make it as long as you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消