0

If you use dependency injection (so you use interfaces) do you set optional parameters only in methods in interface or maybe also in class?

public interface MyInterface
{
    void MyMethod(int count = 5);
}

public class MyClass : MyInterface
{
    public void MyMethod(int count)
    {

    }
}

usage DI:

public class TestClass
{
    private MyInterface _x;

    public TestClass(MyInterface x)
    {
       _x = x;
    }

    public void TestMethod()
    {
       _x.MyMethod();
    }
}

So we can set default parameter only in interface.

michael
  • 647
  • 2
  • 7
  • 17
  • Dependency injection doesn't really make a difference to the use cases; see linked duplicate for explanation of why you'd want to use optional parameters with a default value in an interface. – CodeCaster Jul 04 '14 at 11:34
  • When we use DI we use interfaces so it make sense to set values in interfaces - am I right? – michael Jul 04 '14 at 11:45
  • You don't have to use interfaces for DI and you're not setting a default value for an interface, but for a method parameter... It's not really clear what you're actually asking. Please provide an example of how you would use DI using given interface and how you would use the default value. – CodeCaster Jul 04 '14 at 11:46

0 Answers0