C#/ Lambda:以下参考的param是什么?

Jie*_*eng 5 c# lambda

我正在看这里的代码

/// <summary>
/// Returns the command that, when invoked, attempts
/// to remove this workspace from the user interface.
/// </summary>
public ICommand CloseCommand
{
    get
    {
        if (_closeCommand == null)
            _closeCommand = new RelayCommand(param => this.OnRequestClose());

        return _closeCommand;
    }
}
Run Code Online (Sandbox Code Playgroud)

是什么paramparam => this.OnRequestClose()指什么?

Mar*_*ell 16

RelayCommand可能是一个委托类型,它接受一个参数,或一个本身在构造函数中采用这种委托类型的类型.你正在声明一个匿名方法,简单地说"当被调用时,我们将获取传入的值(但然后不使用它),然后调用OnRequestClose.你也可以(可能更清楚):

_closeCommand = new RelayCommand(delegate { this.OnRequestClose(); });
Run Code Online (Sandbox Code Playgroud)

这可能是在它的其他用途更清晰的使用,例如:

var ordered = qry.OrderBy(item => item.SomeValue);
Run Code Online (Sandbox Code Playgroud)

lambda是"给定的item,获得item's SomeValue".在你的情况下,lambda是"给定param,忽略param和调用OnRequestClose()"