#921 – Objects Are Explicitly Created but Automatically Destroyed

You create an object, or an instance of a classusing the new keyword.  When you create the object, you must also refer to it using a reference-typed variable.

            // Create an instance of a Dog and reference it using
            // the "myDog" variable.
            Dog myDog = new Dog("Kirby", 15);

Using the new keyword, you explicitly create objects.

A;though you create objects explicitly, you never explicitly delete them.  Instead, an object can be deleted after it is no longer being referenced by any reference-typed variables.  Once the object has been deleted, the CLR (Common Language Runtime) will reclaim the memory that was being used by the object.

Not only do you not explicitly delete objects, you also can’t predict when the object will be deleted.  The CLR will decide when to delete the object, based on when it needs the memory that the object is using.

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 #921 – Objects Are Explicitly Created but Automatically Destroyed

  1. Pingback: Dew Drop – September 3, 2013 (#1,616) | Alvin Ashcraft's Morning Dew

  2. Pingback: #924 – You Shouldn’t Normally Worry about Memory Management | 2,000 Things You Should Know About C#

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: