扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:Builder.com.cn 2007年7月31日
关键字: Office
例3——参数的传递
你可以将参数传递给匿名方法,方式就和你处理引用命名方法参数的委托一样。下面的示例3说明这种类型的功能:
示例列表3
#region Parameter example - Example3
privatedelegatevoidExample3(string firstName, string lastName);
privatevoid btnExample3_Click(object sender, EventArgs e)
{
//Setup our parameters.
string parameter1 = "Zach";
string parameter2 = "Smith";
//Create an instance of the Example3 delegate with an
// anonymous method.
Example3 example =
newExample3(
delegate(string firstName, string lastName)
{
MessageBox.Show("Example3: " + firstName + " " + lastName);
});
//Execute the delegate.
example(parameter1, parameter2);
}
#endregion
例4——多个方法的关联
就和命名方法一样,将多个匿名方法与同一个委托进行关联是可能的。这在很多情况下会非常有用——首先想到的是把一个简单的处理程序添加给按钮的点击事件。下面的代码(示例4)显示了一个委托,它同时带有与之相关联一个匿名方法和一个命名方法:
示例列表4
#region Multiple method association (stacking) - Example4
privatedelegatevoidExample4(string firstName, string lastName);
privatevoid btnExample4_Click(object sender, EventArgs e)
{
//Setup our parameters.
string parameter1 = "Zach";
string parameter2 = "Smith";
//Create an instance of the Example4 delegate with an
// anonymous method.
Example4 example =
newExample4(
delegate(string firstName, string lastName)
{
MessageBox.Show("Example4: " + firstName + " " + lastName);
});
//Add another method to the delegate - this time
// a named method.
example += newExample4(Example4NamedMethod);
//Execute the delegate.
example(parameter1, parameter2);
}
privatevoid Example4NamedMethod(string firstName, string lastName)
{
MessageBox.Show("Example4Method: " + firstName + " " + lastName);
}
#endregion
例5——将匿名方法作为参数传递
就和命名方法一样,将匿名方法作为参数传递给函数是可能的。这并不是一个我认为会通常使用的特性,但是我敢肯定未来会有这种需要。下面的代码(例5)说明了这种类型的功能,它将一个命名方法作为参数传递给了函数:
示例列表5
#region Passing anonymous methods - Example5
privatedelegatevoidExample5(string firstName, string lastName);
privatevoid btnExample5_Click(object sender, EventArgs e)
{
//Execute Passit and pass the anonymous method.
Passit((Example5)delegate(string firstName, string lastName)
{
MessageBox.Show("Example5: " + firstName + " " + lastName);
});
//Execute Passit with the named method.
Passit(Example5NamedMethod);
}
privatevoid Example5NamedMethod(string firstName, string lastName)
{
MessageBox.Show("Example5Method: " + firstName + " " + lastName);
}
privatevoid Passit(Example5 example)
{
example("Zach", "Smith");
}
#endregion
例6—-访问类成员
这是对上面例2的变量范围的扩展。但是,这个例子(例6)说明了匿名参数还能够在它们的代码块之外执行命名方法:
示例列表6
#region Accessing class members - Example6
privatedelegatevoidExample6();
privateint _customerId;
privatestring _customerCode;
publicint CustomerID
{
get { return _customerId; }
set { _customerId = value; }
}
publicstring CustomerCode
{
get { return _customerCode; }
set { _customerCode = value; }
}
privatevoid btnExample6_Click(object sender, EventArgs e)
{
//Populate out properties.
this.CustomerID = 90;
this.CustomerCode = "1337HK";
//Setup the delegate/anonymous method.
Example6 example =
newExample6(
delegate
{
this.ShowCustomer(this.CustomerID, this.CustomerCode);
});
//Execute the delegate.
example();
//Change the properties.
this.CustomerID = 54;
this.CustomerCode = "L4M3";
//Execute the delegate again.
// Notice that the new values are reflected.
example();
}
privatevoid ShowCustomer(int customerId, string customerCode)
{
MessageBox.Show(
String.Format("CustomerID: {0} Customer Code: {1}",
customerId, customerCode));
}
#endregion
要注意的是,我两次调用了与匿名方法相关联的委托。你可能会发现一个很有趣的事情:在这些调用中,方法会输出两组不同的值。这是因为用在匿名方法里的外部变量在创建匿名方法的时候被引用。这意味着对这些变量的任何更改都会在匿名函数访问变量的时候被反映出来。
你可能还注意到在这个实例里委托关键字后面没有括号。当匿名方法不需要带参数的时候,后面的括号是可选的。
评论
我希望本文已经说明如何使用匿名方法。虽然它们还不是革命性的,但是它们是C#演化成为一门程序员友好的语言过程中的一个重要步骤。
(文/Zach Smith,builder.com.com)
责任编辑:张琎
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者