c# 3.0 - Nested use of C# Object Initializers -


Thus, object initializers are all easy - especially if you are doing linq, where they are right - But I can not understand this much:

  public class class 1 {public class 2 example; } Public Class Class2 {Public Class 1 Parents; }  

Experiment in this way:

  class 1 square 1 = new square 1 (); Class1.instance = New Class 2 (); Class1.parent = class1; As a beginner  
  class1 class1 = new class1 () {instance = new class2  

This does not work, class1 is considered to be an unusual local The variable is when you are doing something like this, it also becomes intriguing

  select new class 1 () ... ...  

Not to mention it by its name!

How do I see it? Can not I just use object anchors to nested contexts?

Can not I just create nested references using object launchers?

You are right - you can not have a circle; A requires B for initiality, but B requires A before A. To be precise - you can definitely create a nested object initiator, but not with circular dependence.

But you can - and I can suggest that if possible - work like this in this way.

  Public Class A {Public B Child {Receive. This.child; } Set {if (this.child! = Value) {this.child = value; This.child.Parent = this; }}} Private B child = null; } Public Class B {Public A Generate {Receive; } Set {if (this.parent! = Value) {this.parent = value; This.parent.Child = This; }}} Private Parents = Null; }  

There is a profit in building a relationship within the property, if you forget any one of the initial statements, then you can not get an incompatible state. It is quite clear that this is a sub-solution because you need two statements to do one thing.

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

With arguments in properties, you work with only one statement.

  a.child = b;  

Or on the other side.

  b.Parent = a;  

and finally with the initial syntax of the object.

  A = new A = hair = new B ()};  

Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -