RE: Object instantiation in C#

Tech-Archive recommends: Speed Up your PC by fixing your registry



> - 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
.



Relevant Pages

  • Re: C++ design question
    ... >>and policies for object and relationship instantiation (object ... way to do that in OOA/D is via separation of concerns and encapsulation. ... >>conditional one can't implement it with a reference. ... >>policies ensures that the developer thinks about them. ...
    (comp.object)
  • Re: Singletons
    ... There are four basic ways to implement a relationship: embedding an object in the implementation of another object; employing a referential pointer; passing an object reference as a message argument; and using an RDB-style search of instances by explicit identifier. ... Relationships are always implemented and navigated when addressing collaboration messages. ... By modifying the context I meant that one defines the solution flow of control differently so that the instantiation can be done in one place rather than in several. ...
    (comp.object)
  • Re: Managing multiple instances
    ... That will probably mean that you have to have some sort of lookup table somewhere in the implementation. ... You will also need some way to describe the client context so that it can be mapped to the actual object identity (e.g., an index into the lookup table that yields a reference). ... All you have to provide is an enumeration variable that is named for the models. ... Unless instantiation is trivial and unlikely to become more complicated, it is generally good practice to encapsulate the rules and policies for instantiation away from the rules and policies of collaboration. ...
    (comp.object)
  • Re: Abstract public member variales?
    ... Since the direction of these relationships is now clear, if I am correct then it must be that every Deconstructor object has references to ObjectA, ObjectB, and ObjectC, then the various concrete deconstructors choose the appropriate reference to save so that ConcreteA saves ObjectA and ConcreteB saves ObjectB, etc. ... that instantiation is a separate responsibility than the one triggering the actual collaboration activity of saving the object. ... I think that saying this technique uses the Strategy pattern is misleading. ...
    (comp.object)
  • RE: Object instantiation in C#
    ... started in the southeast of Ireland. ... "Dave" wrote: ... > instantiation mechanism, and would be grateful if someone could clarify the ... > - If I declare a property as a user-defined object (ie as a reference) how ...
    (microsoft.public.dotnet.languages.csharp)