什么是从C#中的列表数组中搜索数据的最快方法?在ListArray中搜索C#
我的代码:
public class fruits
{
public string Initial;
public string Fruit;
public fruits(string initials, string names)
{
Initial = initials;
Fruit = names;
}
}
// load
List<fruits> List = new List<fruits>();
List.Add(new fruits("A", "Apple"));
List.Add(new fruits("P", "Pineapple"));
List.Add(new fruits("AP", "Apple Pineapple"));
//combo box select text
var text = combobox.SelectText();
for (int i=0; i<list.Count(); i++)
{
if (list[i].Fruit == text)
{
MessageBox.Show(list[i].Initial);
}
}
我知道这个搜索方法并不好,如果列表数据中包含太多的数据。
“最快”是什么意思? :如果您运行代码,快速开发还是快速? – Fruchtzwerg
@Fruchtzwerg最快≙运行速度最快;最简单≙最快的代码。 – devRicher
如果'Initial'在水果中是唯一的;这可能是更好的使用'Dictionary' –
Sehnsucht