这段代码是什么意思(s,e)?
这段代码是如何工作的?
app.InstallStateChanged += (s, e) => UpdateUI();
NetworkChange.NetworkAddressChanged +=
(s, e) => UpdateNetworkIndicator();
有人可以解读一下吗?
该代码来自 silverlight 4 OOB 系统中使用的示例 http://msdn.microsoft.com/en-us/library/dd833066(v=VS.95).aspx
UpdateNetworkIndicator 不返回任何内容。 UpdateUI 不返回任何内容。
How does this code work?
app.InstallStateChanged += (s, e) => UpdateUI();
NetworkChange.NetworkAddressChanged +=
(s, e) => UpdateNetworkIndicator();
Can someone please unscramble this?
The code comes from an example used in a silverlight 4 OOB systems http://msdn.microsoft.com/en-us/library/dd833066(v=VS.95).aspx
UpdateNetworkIndicator does not return anything.
UpdateUI does not return anything.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是一个包含多个参数的 lambda 表达式。在这种情况下(当您使用该函数替换事件处理程序时)它们相当于
object
和EventArgs
参数。您的代码相当于以下内容
This is a lambda expression that contains multiple parameters. In this case (as you are using the function to replace an event handler) they are equivalent to the
object
andEventArgs
parameters.Your code is equivalent to the below
这是一个 lambda 表达式。
http://msdn.microsoft.com/en-us/library /bb397687.aspx
That's a lambda expression.
http://msdn.microsoft.com/en-us/library/bb397687.aspx
UpdateUI() 和 UpdateNetworkIndicator() 方法都是自定义事件处理程序方法。
+= 运算符将这些事件处理程序分别附加到应用程序和 NetworkChange 触发的事件。
=>表示 lambda 表达式。 (s,e) 是输入参数(在本例中为标准发送者,事件参数),=> 的右侧是是陈述或表达式。
在这种情况下,您可以将其重写为:
并且它应该同样有效。
Both the UpdateUI() and UpdateNetworkIndicator() methods are custom event handler methods.
The += operator is attaching these event handlers to the events fired by the app and NetworkChange respectively.
The => denotes a lambda expression. The (s,e) are input parameters (in this case, the standard sender, event args) and the right of => is the statement or expression.
In this case, you could rewrite this as:
and it should work just as well.
考虑这个例子
简写(使用 lambda 表达式)
是and 的
Consider this example
is the short hand (using lambda expression) for
and
InstallStateChanged 和 NetworkAddressChanged 是事件,您看到的是 lambda 语法,用于定义事件处理程序,以便在触发相应事件时调用 UpdateUI 或 UpdateNetworkIndicator。
InstallStateChanged and NetworkAddressChanged are Events, what you´re seeing is the lambda syntax to define eventhandler to call UpdateUI or UpdateNetworkIndicator if the respective events are fired.
该语法称为 Lambda 表达式。来自 MSDN,
(s, e)
声明创建的委托或表达式树的输入参数。当输入参数是单个时,不需要括号,直接写s =>; ...
。当有多个参数时,需要使用括号 -(s, e) =>
。That syntax is called Lambda Expression. From MSDN,
And the
(s, e)
declare input parameters of created delegate or expression tree. When there's single input parameter, parentheses are not needed and you can write justs => ...
. When there's more than one parameter, parentheses are required -(s, e) =>
.简而言之,s 和 e 指的是函数所针对的属性的对象。代码表达的是:您可以在此函数中访问两种类型的对象。当调用此函数时,从 s 对象获取这个或那个属性,并按以下方式使用它的值。
所以在假设的上下文中
S 是一个字符串
E 是一个 int
(s, e) =>
意味着函数中的进一步引用通过名称 s 和 e 引用相应的对象类型。
(s,e)=> s.Length + e
含义:获取 s 引用的字符串实例的值,并将其添加到 e 引用的 int 实例的值。
In short, the s and e refer to objects whose properties the function is targeting. The code is expressing: you have two types of objects you can access in this function. When this function is invoked, from the s object get this or that property and use its value in the following way.
So in a hypothetical context
S is a string
E is an int
(s, e) =>
Means that further references in the function are referring to those respective object types by the names s and e.
(s , e) => s.Length + e
Means: get the value of the instance of a string to which s refers and add to it the value of the instance of an int to which e refers.