views:

44

answers:

1

I've got the following two classes in C#:

public class MyFirstClass : IMyFirstClass
{
    MySecondClass mySecondClass;
    public MyFirstClass(IMySecondClass mySecondClass)
    {
        this.mySecondClass = mySecondClass;
    }

    public MyFirstClass() : this(new MySecondClass()){}
}

public class MySecondClass : IMySecondClass
{
    MyFirstClass myFirstClass;
    public MySecondClass(IMyFirstClass myFirstClass)
    {
        this.myFirstClass = myFirstClass;
    }

    public MySecondClass() : this(new MyFirstClass()){}
}

You'll notice that when the default constructor for either of these classes is instantiated that the system will crash because of the infinite instantiations that need to take place.

Is there an official term that is used to describe this problem?

+5  A: 

This is known as a circular reference:

A circular reference, sometimes referred to as a run-around, is a series of references where the last object references the first, thus causing the whole series of references to be unusable.

Andrew Hare
awesome! thanks! I had the term "cyclic dependency" in my mind but I didn't think that was it.
mezoid
I really wish it was known as "codependent".
Michael Petrotta