可以访问。Lambda表达式和匿名函数支持闭包(Closure)。编译器在实现的时候,会自动包装一个匿名对象将上下文的变量传进去。但是一般的函数就不支持闭包了,比如:
public void TextShow()
{
TextBox cb_Text = new TextBox();
cb_Text.TextChanged += onchanged;
}void onchanged(obj, args)
{
if(string.isNullOrEmpty(cb_Text.Text)) // error
{
MessageBox.Show("不能为空");
}
}
public void TextShow()
{
TextBox cb_Text = new TextBox();
cb_Text.TextChanged += onchanged;
}void onchanged(obj, args)
{
if(string.isNullOrEmpty(cb_Text.Text)) // error
{
MessageBox.Show("不能为空");
}
}
(obj as TextBox ).Text