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

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

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 comment