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

  • SEARCH
  • Home
  • 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 198295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:55:32+00:00 2026-05-11T16:55:32+00:00

so, object initializers are all kinds of handy – especially if you’re doing linq,

  • 0

so, object initializers are all kinds of handy – especially if you’re doing linq, where they’re downright necessary – but I can’t quite figure out this one:

public class Class1 {
   public Class2 instance;
}

public class Class2 {
   public Class1 parent;
}

using like this:

Class1 class1 = new Class1();
class1.instance = new Class2();
class1.parent = class1;

as an initializer:

Class1 class1 = new Class1() {
    instance = new Class2() {
        parent = class1
    }
};

this doesn’t work, class1 is supposedly an unassigned local variable. it gets even trickier in Linq when you are doing something like

select new Class1() { ...

it doesn’t even have a name to refer to it by!

how do I get around this? can I simply not make nested references using object initializers?

  • 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-11T16:55:32+00:00Added an answer on May 11, 2026 at 4:55 pm

    can I simply not make nested references using object initializers?

    You are right – you cannot. There would be a cycle; A requires B for initialization but B requires A before. To be precise – you can of course make nested object initializers but not with circular dependencies.

    But you can – and I would suggest you should if possible – work this around as follows.

    public class A
    {
       public B Child
       {
          get { return this.child; }
          set
          {
             if (this.child != value)
             {
                this.child = value;
                this.child.Parent = this;
             }
          }
       }
       private B child = null;
    }
    
    public class B
    {
       public A Parent
       {
          get { return this.parent; }
          set
          {
             if (this.parent != value)
             {
                this.parent = value;
                this.parent.Child = this;
             }
          }
       }
       private A parent = null;
    }
    

    Building the relation inside the property has the benifit that you cannot get an inconsitent state if you forget one of the initialization statements. It is quite obvious that this is an suboptimal solution because you need two statements to get one thing done.

    b.Parent = a;
    a.Child = b;
    

    With the logic in the properties you get the thing done with only one statement.

    a.Child = b;
    

    Or the other way round.

    b.Parent = a;
    

    And finally with object initializer syntax.

    A a = new A { Child = new B() };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can i initialize object in my first activity and you it in all activity???
Is it possible to enforce rules or throw an error when using object initializers
I'm using object and collection Initializers in the program and thinking how to get
Using the C# object initializer syntax I can instantiate an anonymous object like this:
How can I use an object initializer with an explicit interface implementation in C#?
Why can't you call methods with c# object initializer syntax? It seems to me
I’ve been trying to get this to work for a while but can’t (I
I understand that Encoding can be used to initialize object to perform any type
So basically i have a domain object and a generic repository that can do
If I use object initializers in using-block I get Code Analysis warning about not

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.