RE: Object instantiation in C#
- From: Brian Delahunty <BrianDelahunty@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 11 Aug 2005 04:05:06 -0700
> - 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?
Check if it is null. All objects in C# default to their default values if
you don't explicitly instantiate/initialize them. The default value for
reference types is null.
e.g.
MyObject obj;
if(obj == null)
{
obj = new MyObject();
}
> - 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)?
I assume you mean something like this:
MyObject object1 = new MyObject();
object1 = new MyObject(); // Reassigning it.
Basically object1 now points to a new object. The old object will be garbage
collected.
Hope this helps.
Ah, you might find this C# for C++ programmers FAQ useful:
http://www.andymcm.com/csharpfaq.htm
--
Brian Delahunty
Ireland
http://briandela.com/blog
INDA SouthEast - http://southeast.developers.ie/ - The .NET usergroup I
started in the southeast of Ireland.
"Dave" wrote:
> 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)?
> --
> Dave
.
- References:
- Object instantiation in C#
- From: Dave
- Object instantiation in C#
- Prev by Date: Re: int array
- Next by Date: Re: Object instantiation in C#
- Previous by thread: Object instantiation in C#
- Next by thread: Re: Object instantiation in C#
- Index(es):
Relevant Pages
|