Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Convert Action to Action<bool>

Tags:

c#

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.

like image 787
Tomtom Avatar asked Oct 08 '12 13:10

Tomtom


2 Answers

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:

  1. var foo = new Foo(x => { Console.WriteLine("Hello"); }); (calls the first ctor)
  2. var foo = new Foo(() => { Console.WriteLine("Hello"); }); (calls the second ctor)
like image 103
Darin Dimitrov Avatar answered Oct 13 '22 05:10

Darin Dimitrov


Action a = () => aboolaction(true);
like image 35
Marco Mp Avatar answered Oct 13 '22 03:10

Marco Mp