Reputation: 167
I am trying to search through a list that contains a structure:
struct Item
{
public string Name;
public Byte[] Data;
}
static List<Item> Items = new List<item>();
Each time I receive the data, bytes change and have to be updated. Is there a way to search for the item that has to be updated, without for each loop?
Also, database is too slow for this - it has to happen in memory (list shouldn't exceed 1MiB)
Thanks
Upvotes: 0
Views: 62
Reputation: 167
Went with dictionary, as suggested by Matthew Watson in the first reply.
static Dictionary<string, Byte[]> Items = new Dictionary<string, Byte[]>();
And updating the list with
Items[currentItem] = GetByteFromData(data);
//and
Items.Remove(currentItem);
Thanks everyone
Upvotes: 1
Reputation: 2306
Linq?
var targetItem = items.First ( x => x.Name == targetValue);
Not sure it is faster than a foreach though. Might be of interest that, unless it has changed recently, for loops are faster than foreach loops.
Upvotes: 0