v11 v11 - 3 months ago 11
C# Question

Using condition expression to choose lambda expression

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

xxxx.UsingFactory(
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
expression'

Answer

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:

xxxx.UsingFactory(
    hasProofing ? (Action)(() => new ProofingA()) : () => new ProofingB()
);
Comments