The current program results in stackoverflow exception which i know why. How could I avoid the circular dependency here. How can i make the three classes independent of each other though the classes are dependent to each other(just imagine that the methods inside those classes refer each other).
namespace CircularDependency_1
{
class Program
{
static void Main(string[] args)
{
A a = new A();
B b = new B();
Console.WriteLine("executed");
Console.ReadLine();
}
}
public class B
{
public A a;
public B()
{
a = new A();
Console.WriteLine("Creating B");
}
}
public class A
{
public B b;
public A()
{
b = new B();
Console.WriteLine("Creating A");
}
}
public class C
{
public A a;
public B b;
public C ()
{
a = new A();
b = new B();
Console.WriteLine("Creating C");
}
}
}