Re: Object instantiation in C#
- From: "Bill Butler" <bill@xxxxxx>
- Date: Thu, 11 Aug 2005 11:50:34 -0400
"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)?
> 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
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
- Follow-Ups:
- Re: Object instantiation in C#
- From: Dave
- Re: Object instantiation in C#
- References:
- Object instantiation in C#
- From: Dave
- Object instantiation in C#
- Prev by Date: Re: Database update
- Next by Date: Re: Freeing Memory
- Previous by thread: RE: Object instantiation in C#
- Next by thread: Re: Object instantiation in C#
- Index(es):
Relevant Pages
|