什么';“的意思是什么=&燃气轮机&引用;在C#中?

什么';“的意思是什么=&燃气轮机&引用;在C#中?,c#,syntax,lambda,C#,Syntax,Lambda,可能重复: 比如说 Messenger.Default.Register<AboutToCloseMessage>(this, (msg) => { if (msg.TheContainer == this.MyContainer) // only care if my container. { // decide whether or not we should cancel t

可能重复:

比如说

Messenger.Default.Register<AboutToCloseMessage>(this, (msg) =>
        {
            if (msg.TheContainer == this.MyContainer) // only care if my container.
            {
                // decide whether or not we should cancel the Close
                if (!(this.MyContainer.CanIClose))
                {
                    msg.Execute(true); // indicate Cancel status via msg callback.
                }
            }
        });
Messenger.Default.Register(此,(消息)=>
{
if(msg.TheContainer==this.MyContainer)//只关心我的容器是否存在。
{
//决定我们是否应该取消交割
如果(!(this.MyContainer.CanIClose))
{
msg.Execute(true);//通过msg回调指示取消状态。
}
}
});
运算符用于Lambda表达式

它允许您“动态”定义匿名函数,并可用于创建委托或表达式树类型。

这是一个lambda表达式(http://msdn.microsoft.com/en-us/library/bb397687.aspx).

它是一个lamda表达式(函数参数)=>{Function Body}


可以指定参数的类型,但编译器通常只解释它。

这就是在C#中定义lambda的方式
msg
是传递给lambda方法的参数,其余是方法的主体

与之相当的是:

Messenger.Default.Register<AboutToCloseMessage>(this, SomeMethod);

void SomeMethod(SomeType msg)
{
    if (msg.TheContainer == this.MyContainer) // only care if my container.
    {
        // decide whether or not we should cancel the Close
        if (!(this.MyContainer.CanIClose))
        {
             msg.Execute(true); // indicate Cancel status via msg callback.
        }
    }
}
Messenger.Default.Register(这个,SomeMethod);
void SomeMethod(SomeType msg)
{
if(msg.TheContainer==this.MyContainer)//只关心我的容器是否存在。
{
//决定我们是否应该取消交割
如果(!(this.MyContainer.CanIClose))
{
msg.Execute(true);//通过msg回调指示取消状态。
}
}
}

它是一个lambda,允许您轻松创建函数

在您的示例中,您还可以编写:

Messenger.Default.Register<AboutToCloseMessage>(this, delegate(Message msg)
{
    if (msg.TheContainer == this.MyContainer) // only care if my container.
    {
        // decide whether or not we should cancel the Close
        if (!(this.MyContainer.CanIClose))
        {
            msg.Execute(true); // indicate Cancel status via msg callback.
        }
    }
});
Messenger.Default.Register(此,委托(消息消息消息)
{
if(msg.TheContainer==this.MyContainer)//只关心我的容器是否存在。
{
//决定我们是否应该取消交割
如果(!(this.MyContainer.CanIClose))
{
msg.Execute(true);//通过msg回调指示取消状态。
}
}
});
甚至

Messenger.Default.Register<AboutToCloseMessage>(this, foobar);

// somewhere after //
private void foobar(Message msg)
{
    if (msg.TheContainer == this.MyContainer) // only care if my container.
    {
        // decide whether or not we should cancel the Close
        if (!(this.MyContainer.CanIClose))
        {
            msg.Execute(true); // indicate Cancel status via msg callback.
        }
    }
}
Messenger.Default.Register(这个,foobar);
//之后某处//
专用void foobar(消息msg)
{
if(msg.TheContainer==this.MyContainer)//只关心我的容器是否存在。
{
//决定我们是否应该取消交割
如果(!(this.MyContainer.CanIClose))
{
msg.Execute(true);//通过msg回调指示取消状态。
}
}
}

@Shakti Singh:你能告诉我们如何搜索
=>
符号吗?@Shakti Singh:如果Rdeluca知道
=>
的意思是“lambda表达式”,他一开始就不会问。是的。。我不知道它叫lambda,否则我会搜索它。谢谢
Messenger.Default.Register<AboutToCloseMessage>(this, foobar);

// somewhere after //
private void foobar(Message msg)
{
    if (msg.TheContainer == this.MyContainer) // only care if my container.
    {
        // decide whether or not we should cancel the Close
        if (!(this.MyContainer.CanIClose))
        {
            msg.Execute(true); // indicate Cancel status via msg callback.
        }
    }
}