使用索引器: class MyCollection { public Dictionary<int, string> dict = new Dictionary<int, string>(); public string this[int key] { get { return dict[i]; } } public int this[string value] { get { return dict.First(x => x.Value == value).Key; } } }
指定条件检索数组元素 private void txt_find_TextChanged(object sender, EventArgs e) { if (txt_find.Text != string.Empty)//判断查找字符串是否为空 { string[] P_str_temp = Array.FindAll//使用FindAll方法查找相应字符串 (G_str_array, (s) => s.Contains(txt_find.Text)); if (P_str_temp.Length > 0)//判断是否查找到相应字符串 { txt_display.Clear();//清空控件中的字符串 foreach (string s in P_str_temp)//向控件中添加字符串 { txt_display.Text += s + Environment.NewLine; } } else { txt_display.Clear();//清空控件中的字符串 txt_display.Text = "没有找到记录";//提示没有找到记录 } } else { txt_display.Clear();//清空控件中的字符串 } }
class MyCollection
{
public Dictionary<int, string> dict = new Dictionary<int, string>();
public string this[int key] { get { return dict[i]; } }
public int this[string value] { get { return dict.First(x => x.Value == value).Key; } }
}
private void txt_find_TextChanged(object sender, EventArgs e)
{
if (txt_find.Text != string.Empty)//判断查找字符串是否为空
{
string[] P_str_temp = Array.FindAll//使用FindAll方法查找相应字符串
(G_str_array, (s) => s.Contains(txt_find.Text));
if (P_str_temp.Length > 0)//判断是否查找到相应字符串
{
txt_display.Clear();//清空控件中的字符串
foreach (string s in P_str_temp)//向控件中添加字符串
{
txt_display.Text += s + Environment.NewLine;
}
}
else
{
txt_display.Clear();//清空控件中的字符串
txt_display.Text = "没有找到记录";//提示没有找到记录
}
}
else
{
txt_display.Clear();//清空控件中的字符串
}
}