如何搜索包含字符串和字节的自定义结构列表 (C#)?
How to search through a custom structure list (C#) containing a string and byte?
我正在尝试搜索包含结构的列表:
struct Item
{
public string Name;
public Byte[] Data;
}
static List<Item> Items = new List<item>();
每次我收到数据时,字节都会改变并且必须更新。有没有一种方法可以搜索必须更新的项目,而无需每次循环?
此外,数据库对于这个来说太慢了 - 它必须发生在内存中(列表不应超过 1MiB)
谢谢
Linq?
var targetItem = items.First ( x => x.Name == targetValue);
虽然不确定它是否比 foreach 快。
可能有兴趣的是,除非最近发生变化,否则 for 循环比 foreach 循环更快。
按照 Matthew Watson 在第一个回复中的建议,使用字典。
static Dictionary<string, Byte[]> Items = new Dictionary<string, Byte[]>();
并使用
更新列表
Items[currentItem] = GetByteFromData(data);
//and
Items.Remove(currentItem);
谢谢大家
我正在尝试搜索包含结构的列表:
struct Item
{
public string Name;
public Byte[] Data;
}
static List<Item> Items = new List<item>();
每次我收到数据时,字节都会改变并且必须更新。有没有一种方法可以搜索必须更新的项目,而无需每次循环?
此外,数据库对于这个来说太慢了 - 它必须发生在内存中(列表不应超过 1MiB)
谢谢
Linq?
var targetItem = items.First ( x => x.Name == targetValue);
虽然不确定它是否比 foreach 快。 可能有兴趣的是,除非最近发生变化,否则 for 循环比 foreach 循环更快。
按照 Matthew Watson 在第一个回复中的建议,使用字典。
static Dictionary<string, Byte[]> Items = new Dictionary<string, Byte[]>();
并使用
更新列表Items[currentItem] = GetByteFromData(data);
//and
Items.Remove(currentItem);
谢谢大家