v11 v11 - 1 year ago 117
C# Question

Using condition expression to choose lambda expression

I want to use condition expression to choose lambda expression, like that:

hasProofing? ( ()=>new ProofingA() ) : ( () => new ProofingB() )

But, it show me errors. So, if I want to do this thing, How should I do.

Error Detail:

no implicit conversion between 'lambda expression' and 'lambda

Answer Source

You need to explicitly cast at least one of the lambdas. For example, if it's just a Action, then you could use the following:

    hasProofing ? (Action)(() => new ProofingA()) : () => new ProofingB()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download