委托可以有可选参数吗?
我的下面的代码工作正常,直到我尝试添加 bool NetworkAvailable = true
部分。现在,我在下面的第 4 行收到 Method name Expected
编译时异常。
void NetworkStatus_AvailabilityChanged(object sender, NetworkStatusChangedArgs e)
{
var networkAvailable = e.IsAvailable;
SetUpdateHUDConnectedMode d = new SetUpdateHUDConnectedMode(UpdateHUDConnectedMode(networkAvailable));
this.Invoke(d);
}
delegate void SetUpdateHUDConnectedMode(bool NetworkAvailable = true);
private void UpdateHUDConnectedMode(bool NetworkAvailable = true)
{
...
}
诚然,我对委托
和可选参数
很陌生,所以我将不胜感激任何见解。谢谢。
I have the below code that was working fine until I tried adding the bool NetworkAvailable = true
portion. Now I get a Method name expected
compile time exception at Line 4 below.
void NetworkStatus_AvailabilityChanged(object sender, NetworkStatusChangedArgs e)
{
var networkAvailable = e.IsAvailable;
SetUpdateHUDConnectedMode d = new SetUpdateHUDConnectedMode(UpdateHUDConnectedMode(networkAvailable));
this.Invoke(d);
}
delegate void SetUpdateHUDConnectedMode(bool NetworkAvailable = true);
private void UpdateHUDConnectedMode(bool NetworkAvailable = true)
{
...
}
I am, admittedly, new to Delegates
and Optional Parameters
so I would be grateful for any insight. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在某种非常有限的程度上。使用 C# 4:
然后你可以调用
To some very limited extent. Using C# 4 :
And then you can call
委托指向方法定义。
当您实例化指向方法的委托时,您不能指定任何参数。
相反,您需要将参数值传递给
Invoke
方法,如下所示:A delegate points to a method definition.
When you instantiate a delegate pointing to a method, you cannot specify any parameters.
Instead, you need to pass the parameter values to the
Invoke
method, like this: