在上一篇[匿名方法]的用处多(妙用)
这次发现有更简单的方法ambda表达式
于是
下面的这部分代码:
foreach(Control control in Controls)
{
string tmp = listControlType.Find(
delegate(string s){
if(s == control.GetType().ToString())
return true;
else
return false;
}
);
if(tmp == default(string))
{
listControlType.Add(c.GetType().ToString());
}
}
可以用Lambda改写为:
foreach (Control c in Controls)
{
string tmp = listControlType.Find(s => s == c.GetType().ToString());
if(tmp == default(string))
{
listControlType.Add(c.GetType().ToString());
DicControlTypeName.Add(c.Name, c.GetType().ToString());
comboBox1.Items.Add(c.GetType().ToString());
}
}
于是
下面的这部分代码:
foreach(Control control in Controls)
{
string tmp = listControlType.Find(
delegate(string s){
if(s == control.GetType().ToString())
return true;
else
return false;
}
);
if(tmp == default(string))
{
listControlType.Add(c.GetType().ToString());
}
}
可以用Lambda改写为:
foreach (Control c in Controls)
{
string tmp = listControlType.Find(s => s == c.GetType().ToString());
if(tmp == default(string))
{
listControlType.Add(c.GetType().ToString());
DicControlTypeName.Add(c.Name, c.GetType().ToString());
comboBox1.Items.Add(c.GetType().ToString());
}
}