C # Array or Collection

How would you approach this? I have a list of data that has two things that should be stored in price and position, is there anyway to store this in an array, not knowing the total number of rows that I will have? Or should I use a collection if they can be multidimensional?

thank

+3
source share
4 answers

Yes - I would recommend you look at the general list. Using this, you can simulate your data and store as many elements as possible.

for instance

  public class PricedItem
    {
        public decimal Price { get; set; }
        public object Item { get; set; }
    }

They can then be saved / retrieved to List<PricedItem> - or as a "list of rated items".

Hope that helps

+6
source

, "", , .

, . , , id, name, description, price, ,

Dictionary<int, item> items = new Dictionary<int, item>();
items.add(myitem.id, myitem);

,

string description = items[id].description; 
decimal price = items[id].price;
+2

List<> - , . , KeyValuePair<>, () (). A List<KeyValuePair<string, string>>, Linq , . , KeyValuePairs, Key, , , .

+2
source

you may have

struct Item
{
    float Price;
    string ItemName;
}

and

List<Item> items = new List<Item>();
items.Add(new Item{Name="item1", Price=19.99});
0
source

Source: https://habr.com/ru/post/1793650/


All Articles