Lets say I have the following:
public interface IFoo
{
void Foo();
}
public interface IBar
{
void Bar();
}
public class FooAndBar : IFoo, IBar
{
//valid implementation
}
Now I have a class that takes in an IFoo and an IBar in the constructor, but has a paramter-less constructor as well.
public class Consumer
{
private IFoo foo;
private IBar bar;
public Consumer(IFoo foo, IBar bar)
{
this.foo = foo;
this.bar = bar;
}
public Consumer() : this(new FooAndBar(), new FooAndBar()) {}
}
I want to maintain the relationship between the paramter-less constructor calling the parameterized version. But I would like to pass in one referenced object rather than ‘new’ing up two FooAndBar instances. Is there anyway to do this while still maintaining the relationship between the constructors?
You can create an interim private constructor that takes a FooAndBar as a parameter: