Re: Object instantiation in C#

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance




"Dave" <Dave@xxxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:A1975CD2-F14B-4CDF-AD4B-831A54F8702B@xxxxxxxxxxxxx...
>I come form a C++ background and am new to C#. I am puzzled by the object
> instantiation mechanism, and would be grateful if someone could clarify the
> following related questions:
> - If I declare a property as a user-defined object (ie as a reference) how
> can I test to see if it has been instantiated (assigned) or not?
> - If I use 'new' to instantiate an object, but the reference variable I use
> already refers to an instantiation of the class, is the reference variable
> reassigned, and if so what happens to the object it previously referenced
> (which is now presumably completely inaccessible)?
 Hi Dave,
 
The other posters answered you question, but let me go a step further.
 
Example 1:
 A a = new A("Foo");        //  a is a reference to an instance of A that we will call Foo
 a = new A("Bar");          //  a is now a reference to an instance of A that we will call Bar
                            //  Foo is eligible for garbage collection
 
 
Example 2:
  A a = new A("Foo");       //  a is a reference to Foo
  B b = new B("qwerty");    //  b is a reference to an instance of B that we will call qwerty
  b.X = a;                  //  X is a property of type A
  a = new A("Bar");         //  a is now a reference to Bar
                            //  Foo is NOT eligible for garbage collection
                            //  since qwerty still has a reference to FOO
                           
Example 3:
  A a = new A("Foo");       //  a is a reference to Foo
  B b = new B("qwerty");    //  b is a reference to qwerty
  b.X = a;                  //  X is a property of type A
  a = new A("Bar");         //  a is now a reference to Bar
  b = null;                 //  there is no reference to qwerty
                            //  Foo is eligible for garbage collection
                            //  even though qwerty still has a reference to Foo
                            //  qwerty is also eligible for garbage collection
                           
Example 4:
  A a = new A("Foo");       //  a is a reference to Foo
  B b = new B("qwerty");    //  b is a reference to qwerty
  b.X = a;                  //  X is a property of type A
  a.Y = b;                  //  Y is a property of type B
  a = null                  //  a now refers to nothing
  b = null;                 //  b now refers to nothing
                            //  Foo and qwerty are both eligible for GC
                            //  even though they both refer to each other.
                            //  This is because there is no "Reachable" reference to either one.
 
 Hope this helps
          Bill


Relevant Pages

  • Re: How to destroy arrays
    ... > garbage collection. ... When a is an array reference, ... > So, in my example, because Foo is not used after ... >> which Scott did not write). ...
    (microsoft.public.dotnet.general)
  • Re: Threads preventing garbage collection?
    ... Ara Howard wrote: ... I don't think this should prevent garbage collection of Foo, ... is holding a reference to Foo ...
    (comp.lang.ruby)
  • Re: 7.0 wishlist?
    ... The "auto-implement" is intended mainly for the odd situation where an existing class you can't edit fits some interface and you're willing to take responsibility for it if it turns out not to actually adhere to the contract, and try using it where that interface type is expected. ... If reference declarations started showing up with the odd asterisk, bang, or other punctuation mark on it, but never primitive declarations, people would probably be able to guess what was going on, on the basis of "what other binary flag might be set on references but not primitives and would be really useful besides can be/cannot be null?" ... the compiler cannot prove by static analysis that the RHS isn't null might be a good idea. ... Object foo, bar; ...
    (comp.lang.java.programmer)
  • Re: 7.0 wishlist?
    ... This is a slight modifier that would be very common on reference declarations, so it would be good to economize on typing or visual clutter where it was used. ... which has the enum constants actually singleton instances of same-named subclasses. ... enum Foo ... The point being to allow to publish an interface ...
    (comp.lang.java.programmer)
  • Re: C++ design question
    ... >>void DoStuffWithBarBase ... I assumed that the Foo subclasses each had specialized responsibilities, ... policies for object and relationship instantiation (object ... conditional one can't implement it with a reference. ...
    (comp.object)