#922 – Ways in Which References to an Object Are Released

An object will potentially be deleted after there are no longer any references to the object.  You reference an object using a reference-typed variable.

There are several different ways in which a reference to an object can be released.

  • Change the reference-typed variable so that it refers to a different object
  • Change the reference-typed variable so that it refers to null
  • Let the reference-typed variable go out of scope (return control from the method in which the variable was declared)
        static void SomeMethod()
        {
            // myDog references the Kirby Dog object
            Dog myDog = new Dog("Kirby");

            // Refer to new Dog object,
            // no longer refer to Kirby Dog object
            myDog = new Dog("Jack");

            // Set reference to null,
            // no longer refer to Jack Dog object
            myDog = null;

            myDog = new Dog("Ruby");

            // When we leave the method, myDog
            // will go out of scope and no longer
            // refer to Ruby Dog object
            return;
        }
Advertisements

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

2 Responses to #922 – Ways in Which References to an Object Are Released

  1. monomorpher says:

    Basic & Functional!
    I’m looking forward to see/play with detailed memory management in coming #’s.
    I’m glued here.

  2. Agent117 says:

    Reblogged this on Phani Kumar Tadepally.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: