Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3228394
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:41:14+00:00 2026-05-17T16:41:14+00:00

I am developing a C# application and have an Employee class and an Organisation

  • 0

I am developing a C# application and have an Employee class and an Organisation class.

An Employee object has an Organisation as an internal member and an Organisation object has an Employee member to indicate the Org leader.

Will there be any problems with this setup that can lead to an infinite circular instantiations?

Edit:

I just tried it running the code and there seems to be a problem. The employee object instantiates an Organisation object and an Organisation object tries to instantiate an employee object. They both connect to a database to fill in their details

This keeps happening until my SQL server runs out of connections. Is there any alternative to what I am doing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T16:41:14+00:00Added an answer on May 17, 2026 at 4:41 pm

    Will there be any problems with this setup that can lead to an infinite circular instantiations?

    Reference types are null by default. Unless you instantiate a new instance and assign it to that variable, then no code is called for that type.

    Do each of the constructors construct an instance of the other object? Is it possible that they could in the future (possibly on accident)? If so, then yes, you can hit the scenario you are talking about:

    class A
    {
      public A()
      {
        this.B = new B();
      }
      public B B;
    }
    
    class B
    {
      public A A = new A(); // This is the same as instantiating the object in the ctor
    }
    
    // ...
    
    A obj = new A(); // This calls new B, which calls new A, repeat ad infinitum
    

    You can break a cycle like this by instantiating the instance of the other object outside of the constructor. For example, on first access:

    class A
    {
      public A()
      {
      }
    
      public B B
      {
        get
        {
          if(b == null)
            b = new B();
          return b;
        }
      }
    
      private B b;
    }
    
    class B
    {
      public B()
      {
      }
    
      public A A
      {
        get
        {
          if(a == null)
            a = new A();
          return a;
        }
      }
    
      private A a;
    }
    
    // ...
    
    A obj = new A(); // new B doesn't get called yet
    obj.B.Something(); // ... Now it does...
    

    You’ll still have to be careful that class A doesn’t access it’s own this.B property inside its constructor, and vice-versa. You can access these properties in methods all you want, though, as long as those methods aren’t called inside the constructor.

    Another option is to do dependency injection. This is where you pass a dependency to an object, instead of letting it instantiate the object itself:

    class A
    {
      public A(B b)
      {
        this.B = b;
      }
    
      public B B;
    }
    
    class B
    {
      public B(A a)
      {
        this.A = a;
      }
    
      public A A;
    }
    
    // ...
    
    A someA = new A(
      new B(
        null)); // You have to break the cycle somewhere...
    someA.B.A = someA;
    
    // or ...
    
    class ABFactory
    {
      public static A CreateA(/* options for a */, /* options for b */)
      {
        A result = new A(
          new B(
            null));
        result.B.A = result;
        return result;
      }
    }
    

    Note that dependency injection wasn’t invented to solve this problem, but it would work in this scenario. With it, you could rest easy, knowing that you would never hit this issue again.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing a web application which has Chart Controls. I have developed a
I'm developing an application which currently have hundreds of objects created. Is it possible
I am developing an application, and have URLs in the format www.example.com/some_url/some_parameter/some_keyword . I
I am developing an application using MVC Preview 5. I have used typed views.
I am (unfortunately) developing an application in Excel 2000 VBA. I believe I have
I have this application I'm developing in JSP and I wish to export some
I am developing an iOS application, and trying to zip the file I have
I am developing a web application using Struts 2.1.2 and Hibernate 3.2.6.GA. I have
Hey I am developing an desktop application using Spring and Hibernate, and I have
Hi I am developing a simple application based upon ASP.NET MVC. I have altered

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.