Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#/Lambda: What does param in the following refer to?

Tags:

c#

lambda

I am looking at the code from here

/// <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;
    }
}

what does param in param => this.OnRequestClose() refer to?

like image 455
Jiew Meng Avatar asked Oct 06 '10 06:10

Jiew Meng


2 Answers

RelayCommand is presumably a delegate-type that accepts a single parameter, or a type that itself takes such a delegate-type in the constructor. You are declaring an anonymous method, saying simply "when invoked, we'll take the incoming value (but then not use it), and call OnRequestClose. You could also have (maybe clearer):

_closeCommand = new RelayCommand(delegate { this.OnRequestClose(); });

It is probably clearer in other uses where it is used, for example:

var ordered = qry.OrderBy(item => item.SomeValue);

where the lambda is "given an item, obtain the item's SomeValue". In your case the lambda is "given param, ignore param and call OnRequestClose()"

like image 70
Marc Gravell Avatar answered Sep 17 '22 18:09

Marc Gravell


param => this.OnRequestClose() is lambda

Func<sometype_that_param_is,sometype_that_OnRequestClose_Is>

or Action

I'm not sure which

So it's just an expression for a func that is called by something, that will pass an argument which will be 'param' an then not used

like image 40
Luke Schafer Avatar answered Sep 19 '22 18:09

Luke Schafer