I have a class that has the following property:
public Action<bool> Action { get; private set; }
And I have a constructor that takes Action<bool>
as an argument.
Now I want to add another constructor that accepts an object of type Action
. How can I convert Action
to Action<bool>
? The bool-parameter should be true in this case.
public class Foo
{
public Foo(Action<bool> action)
{
// Some existing constructor
}
public Foo(Action action): this(x => action())
{
// Your custom constructor taking an Action and
// calling the existing constructor
}
}
Now you could instantiate this class in 2 ways depending on which one of the 2 constructors you want to invoke:
var foo = new Foo(x => { Console.WriteLine("Hello"); });
(calls the first ctor)var foo = new Foo(() => { Console.WriteLine("Hello"); });
(calls the second ctor)Action a = () => aboolaction(true);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With