passing a method as a constructor's parameter

12,669

Solution 1

I guess the TheClass constructor accepts an Action delegate:

public class TheClass
{
    private readonly Action _action;
    public TheClass(Action action)
    {
        _action = action;
    }
}

This way TheClass can execute the provided action at a later time, and more than once.

For example:

public void DoAction()
{
    _action();
}

You could also do:

var theClass = new TheClass(() => TheMethod());

Solution 2

There are many reasons that you might want to pass a method into the constructor of a class.

One of the most important is for so-called Dependency Injection, where want to inject a dependency into a class.

Suppose that a class needs to create an object. Normally, that class would do something like var item = new MyConcreteClass();.

However, that creates a strong dependency between the class creating the object and MyConcreteClass. This can make it harder to unit test or change implementations.

To circumvent this problem, you can do this:

  1. Extract from MyConcreteClass an interface (say IMyInterface) which contains all the things you need to use from inside the class being written.
  2. Pass to the class constructor a Func method which creates and returns a concrete class as IMyInterface.
  3. Inside the class being written, call that Func to create the object rather than creating it directly using new.

The Func is therefore acting as a factory.

Code might look like this:

using System;

namespace Spacelabs.WcfDuplexDemo.Client
{
    static class Program
    {
        static void Main()
        {
            var myClass = new MyClass(() => new MyConcreteClass());

            myClass.DoSomething();
        }
    }

    public interface IMyInterface
    {
        string MyMethod(int param);
    }

    public sealed class MyConcreteClass : IMyInterface
    {
        public string MyMethod(int param)
        {
            return param.ToString();
        }
    }

    public sealed class MyClass
    {
        private readonly Func<IMyInterface> createMyInterface;

        public MyClass(Func<IMyInterface> createMyInterface)
        {
            this.createMyInterface = createMyInterface;
        }

        public void DoSomething()
        {
            // Instead of var item = new MyConcreteClass(), we do the following:

            var item = createMyInterface();
            Console.WriteLine(item.MyMethod(12345));
        }
    }
Share:
12,669
Mahmood Shahrokni
Author by

Mahmood Shahrokni

Updated on June 16, 2022

Comments

  • Mahmood Shahrokni
    Mahmood Shahrokni about 2 years

    While I was looking at some c# code I found a new class declaration which had passed a method as a constructor’s parameter.This is my first time I have seen something like this and it has no meaning for me .Although the whole application works fine I am still interested to know what is the main idea behind the scene.

    var c = new List<TheClass>
    {
        new TheClass(TheMethod);
    }
    
    private void TheMethod()
    {
        //do somthing
    }