简体   繁体   中英

Lambda Function Using Unknown Parameter

I see PRISM declaring the following constructor, and I don't understand what's that "o" being used with the lambda function that serves as the second parameter when the base constructor is called:

public DelegateCommand(Action<T> executeMethod)
    : this(executeMethod, (o)=>true)
{            
}

I'd appreciate an explanation.

The constructor which declaration you posted calls another constructor, so to explain it, we should first look at the other constructor's signature:

public DelegateCommand(Action<T> executeMethod, Func<T, bool> canExecuteMethod)

So the second parameter is a Func<T, bool> . That means it is a function that takes a parameter of type T and returns a boolean.

Now if you look at the lambda that is used:

(o) => true

Lambdas in general have the syntax (parameter-list) => lambda-body , so in this case, the single parameter of the lambda is a variable o (which type is inferred to be T ) and the function returns a constant result true .

The purpose of this is to basically make a command that is always executable.

Of course that lambda could look a lot more complicated, so when using the DelegateCommand, you are likely to use more complex and non-constant expressions. For example:

 new DelegateCommand(DoSomething, o => o.SomeProperty >= 0 && o.SomeProperty < 10 && o.SomeBoolProperty)

It calls this constructor:

DelegateCommand<T>(Action<T>, Func<T, Boolean>)

Passing a lambda that always returns true as the second parameter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM