#44 – Multiple References to the Same Object

If you assign a variable that points to a reference type to another variable of the same type, both variables end up pointing to the same object in memory.  This means that changing the contents of the object through the first reference results in changes that are also seen by the second reference.

The following code:

 Person p1 = new Person("Sean", 46);     // New Person object
 Person p2 = p1;                         // Points to same object

Results in this situation:

Also, if we change one of the properties of the Person object using the p2 reference, we see the same change when using p1. This confirms that both variables are pointing to the same object.

 p2.Age = 50;
 int age = p1.Age;       // Also now = 50
Advertisement

#43 – Objects Are Instantiated on the Heap

When you instantiate a reference type using the new operator, you are creating a new instance of that type.  The object is created on what’s known as the managed heap.  In other words, memory is allocated to store the member data of the object and that memory is allocated from an area of memory known as the heap.

For example, when you execute the following line to create a new Person object:

  Person myPerson = new Person("Sean", 46);

You’ve created an instance of a Person, storing the data on the heap.  You’ve also created a reference variable, myPerson, that references (or points to) the new object.

#42 – Interacting with an Object

Once you create an instance of a reference type using the new keyword, you can interact with the new object through the variable that serves as a reference to that object.  You can call the object’s methods or read and write its properties.

 // Make a new Person object
 Person p1 = new Person("Sean", 46);

 // Read some properties
 string theName = p1.Name;   // Sean
 int howOld = p1.Age;        // 46

 // Set Age property to new value
 p1.Age = p1.Age - 10;
 int younger = p1.Age;       // Now 36

 // Call method that takes no parameters, returns description
 //   Will return:  Sean is 36 yrs old.
 string describe = p1.Description();

#41 – Instantiating Reference Types Using the new Keyword

To create an object of a particular type, you need to instantiate the type.  Value types are instantiated by assigning them a value.  Reference types are instantiated using the new keyword.  Using new allows us to create a new instance of the type.

When new is used to instantiate a type, the type’s constructor is called to perform the initialization.  The type may have a default constructor that takes no parameters, or it may have a constructor that takes one or more parameter values.  It may also support multiple constructors.

Variables declared as instances of reference types will hold the value null until they are instantiated.

 Person p1;     // Not instantiated, value is null

 // p2 points to new instance of the Person class
 //   Default constructor, takes no parameters
 Person p2 = new Person();

 // Construct another Person object using a
 //   different constructor, which takes Name and Age
 Person p3 = new Person("Sean", 46);

#31 – Value Types and Reference Types

All types in C#, whether built-in or user-defined custom types, can be categorized as either value types or reference types.

  • Value types
    • Derive from System.ValueType (which derives from System.Object)
    • Allocated on the stack  (unless declared inside a reference type)
    • Value type variable contains value directly
    • Assignment makes a copy of the value
    • Passed by value (a copy is made)
    • Not garbage collected–die when they go out of scope
    • Either struct or enum
    • Sealed–can’t inherit from them
  • Reference types
    • Derive from System.Object or another reference type
    • Allocated on the heap
    • Reference type variable contains a reference (pointer) to the object’s contents (or contains null)
    • Assignment creates a new reference to the original object
    • Passed by reference  (pointer to object is passed)
    • Garbage collected
    • One of: class, delegate, array or interface
    • Support inheritance