tags:

views:

139

answers:

4

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?

+15  A: 

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()"

Marc Gravell
This is a good answer, maybe clearer still if it's written out explicitly as `delegate(object param) { this.OnRequestClose(); }`
Dave
@Dave - no, that was a very deliberate choice to show a syntax that is (IMO) much clearer **when you don't use the argument**. I'd actually use the lambda version (as per the original question) in preference to `delegate(object param) {...}`
Marc Gravell
@Marc - I wouldn't use the explicit form from my comment above either, I just intended it as an explanatory device to show the meaning of `param` in the lambda form.
Dave
+1  A: 

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

Luke Schafer
+1  A: 

Nothing. The line defines a lambda expression representing a function. That function have a signature like: Foo(T param) where T will be a specific type infered by the compiler based on the type of the argument of the construtor being called.

Rune FS
+1  A: 

param is the only parameter of your lambda expression ( param=>this.OnRequestClose() )

As you are instantiating an ICommand object, param would probably contain the parameter passed to this ICommand from the UI. In your case, the parameter is not used in the command (it does not appear on the right side of the lambda expression).

Maupertuis