这段代码是什么意思(s,e)?

发布于 2024-12-02 08:55:58 字数 472 浏览 2 评论 0原文

这段代码是如何工作的?

      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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

嘿嘿嘿 2024-12-09 08:55:58

这是一个包含多个参数的 lambda 表达式。在这种情况下(当您使用该函数替换事件处理程序时)它们相当于 objectEventArgs 参数。

您的代码相当于以下内容

app.InstallStateChanged += OnInstallStateChanged;
NetworkChange.NetworkAddressChanged += OnNetworkAddressChanged;

/* ... */

private void OnInstallStateChanged(object s, EventArgs e)
{
    UpdateUI();
}

private void OnNetworkAddressChanged(object s, EventArgs e)
{
    UpdateNetworkIndicator();
}

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 and EventArgs parameters.

Your code is equivalent to the below

app.InstallStateChanged += OnInstallStateChanged;
NetworkChange.NetworkAddressChanged += OnNetworkAddressChanged;

/* ... */

private void OnInstallStateChanged(object s, EventArgs e)
{
    UpdateUI();
}

private void OnNetworkAddressChanged(object s, EventArgs e)
{
    UpdateNetworkIndicator();
}
浅浅 2024-12-09 08:55:58

这是一个 lambda 表达式。

“所有 lambda 表达式都使用 lambda 运算符 =>,读作
“去”。 lambda 运算符的左侧指定输入
参数(如果有),右侧保存表达式或
语句块。 lambda 表达式 x => x * x 读作“x 转到 x
乘以 x。”

http://msdn.microsoft.com/en-us/library /bb397687.aspx

That's a lambda expression.

"All lambda expressions use the lambda operator =>, which is read as
"goes to". The left side of the lambda operator specifies the input
parameters (if any) and the right side holds the expression or
statement block. The lambda expression x => x * x is read "x goes to x
times x."

http://msdn.microsoft.com/en-us/library/bb397687.aspx

绿萝 2024-12-09 08:55:58

UpdateUI() 和 UpdateNetworkIndicator() 方法都是自定义事件处理程序方法。

+= 运算符将这些事件处理程序分别附加到应用程序和 NetworkChange 触发的事件。

=>表示 lambda 表达式。 (s,e) 是输入参数(在本例中为标准发送者,事件参数),=> 的右侧是是陈述或表达式。

在这种情况下,您可以将其重写为:

app.InstallStateChanged += UpdateUI;
NetworkChange.NetworkAddressChanged += UpdateNetworkIndicator;

并且它应该同样有效。

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:

app.InstallStateChanged += UpdateUI;
NetworkChange.NetworkAddressChanged += UpdateNetworkIndicator;

and it should work just as well.

聽兲甴掵 2024-12-09 08:55:58

考虑这个例子

Button1.Click += (s, e) => log(e);

简写(使用 lambda 表达式)

Button1.Click += new EventHandler(Button1_Click);

是and 的

void Button1_Click(object sender, EventArgs e)
{
    log(e);
}

Consider this example

Button1.Click += (s, e) => log(e);

is the short hand (using lambda expression) for

Button1.Click += new EventHandler(Button1_Click);

and

void Button1_Click(object sender, EventArgs e)
{
    log(e);
}
半透明的墙 2024-12-09 08:55:58

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.

谁把谁当真 2024-12-09 08:55:58

该语法称为 Lambda 表达式。来自 MSDN,

lambda 表达式是一个匿名函数,可以包含
表达式和语句,可用于创建委托或
表达式树类型。

(s, e) 声明创建的委托或表达式树的输入参数。当输入参数是单个时,不需要括号,直接写 s =>; ...。当有多个参数时,需要使用括号 - (s, e) =>

That syntax is called Lambda Expression. From MSDN,

A lambda expression is an anonymous function that can contain
expressions and statements, and can be used to create delegates or
expression tree types.

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 just s => .... When there's more than one parameter, parentheses are required - (s, e) =>.

甜`诱少女 2024-12-09 08:55:58

简而言之,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文