Team LiB
Previous Section Next Section

Reference Types

Reference types are instances of classes in C#. These are found on the managed heap. To create a new instance of a class reference type, use the new keyword.

Note 

One of the great things about C# is that you no longer need to take care of cleanup for the object by using delete. The garbage collector does this for you automatically when the object is no longer reachable. The garbage collector is discussed in later chapters.

Inheriting from System.Object

All reference types inherit from System.Object. If you were to dump the MethodTable (discussed in Chapter 16), you would see an object is made up of the following:

0:000> !dumpmt -MD 00932140
EEClass : 06be6a90
Module : 0015ba48
Name: System.Object
mdToken: 02000002 (c:\windows\microsoft.net\framework\v1.1.4322\mscorlib.dll)
MethodTable Flags : 2080000
Number of IFaces in IFaceMap : 0
Interface Map : 009321a4
Slots in VTable : 14
-------------------------------------
MethodDesc Table
  Entry MethodDesc   JIT
      Name
0093203b 00932040   None
[DEFAULT] [hasThis] String System.Object.ToString()
      009320fb 00932100   None
[DEFAULT] [hasThis] Boolean System.Object.Equals(Object)
      791f4a07 791f4a0c   None
[DEFAULT] [hasThis] Boolean System.Object.Equals(Object)
      0093207b 00932080   None
[DEFAULT] [hasThis] Void System.Object.Finalize()
      0093202b 00932030   None
[DEFAULT] [hasThis] Void System.Object..ctor()
      009320cb 009320d0 None
[DEFAULT] [hasThis] Class System.Type System.Object.InternalGetType()
      009320e3 009320e8   None
[DEFAULT] [hasThis] Class System.Type System.Object.FastGetExistingType()
      0093204b 00932050   None
[DEFAULT] Boolean System.Object.Equals(Object,Object)
      0093205b 00932060   None
[DEFAULT] Boolean System.Object.ReferenceEquals(Object,Object)
      0093206b 00932070   None
[DEFAULT] [hasThis] Class System.Type System.Object.GetType()
      0093212b 00932130   None
[DEFAULT] [hasThis] Object System.Object.MemberwiseClone()
      0093208b 00932090   None
[DEFAULT] [hasThis] Void System.Object.FieldSetter(String,String,Object)
      0093209b 009320a0   None
[DEFAULT] [hasThis] Void System.Object.FieldGetter(String,String,ByRef Object)
      009320ab 009320b0   None
[DEFAULT] [hasThis] Class System.Reflection.FieldInfo
System.Object.GetFieldInfo(String,String)

The following example shows the use of reference types.

Code Example: Reference Type
Start example
using System;
namespace MyNamespace
{
        class MyMainClass
        {
               static void Main(string[] args)
               {
                      My2ndMainClass My2ndClass = new My2ndMainClass();
               }
        }
        class My2ndMainClass
        {
               public void MyFunction()
               {
                      int MyInt = 0;
               }
        }
}
End example

Converting Reference Types: Boxing and Unboxing

Boxing describes a situation where a value type needs to be converted to a reference type. When this is done, an object is created to hold the value and the value is copied into the object.

Unboxing is the reverse of boxing. In this situation, the object created when the value type was boxed is copied back into a value type.

Caution 

Be very careful when you box and unbox, because these actions can cause the managed heap to be thrashed. Every time you box an object, you force the object to be copied to the heap. When you unbox it, the object is moved back to the stack. You will most often see this when you call methods that accept an Object as the type and you are passing a value type. If you try to unbox an underlying value type, and the type you are unboxing to is not compatible with the type you boxed, you will get InvalidCastException.

The following shows an example of boxing and unboxing.

Code Example: Boxing and Unboxing
Start example
using System;
namespace Client.Chapter_1___Common_Type_System
{
      class BoxandUnbox
      {
            static void Main(string[] args)
            {
                  long MyLong = 1005;
                  //Boxes
                  object MyObject = MyLong;
                  //Unboxes
                  long MyLong2 = (long)MyObject;
            }
      }
}
End example

Team LiB
Previous Section Next Section