Reference types

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
Exam Prep. Guides
Exam 70-536 Study Guide

1. Types and collections

2. Process, threading,…
3. Embedding features
4. Serialization, I/O
5. .NET Security
6. Interop., reflection,…
7. Global., drawing, text

edit

Contents


The Basics

Reference types are more commonly referred to as objects. They include instances Classes, Interfaces, Delegates and arrays, as well as the built-in reference types System.Object and System.String. Variables of type reference do not hold the actual data for the object (like value types); instead they hold a reference to the data. The data is allocated on the heap and is automatically garbage collected when it is no longer in use.

The object type is an alias for Object in the .NET Framework. In the unified type system of C#, all types, predefined and user-defined, reference types and value types, inherit directly or indirectly from Object. You can assign values of any type to variables of type object. When a variable of a value type is converted to object, it is said to be boxed. When a variable of type object is converted to a value type, it is said to be unboxed. For more information, see Boxing and Unboxing.

Copying reference types

Assigning a variable of type reference to another variable of type reference copies that reference (it tells the new object where the place in memory is), but does not make a copy of the object. In order to copy the values held by a reference type, a deep copy is required. That is to say, each item in the reference type must be manually copied. This can be accomplished in several ways, with some of the system types providing easy methods.


Passing references

Unlike value types, there is no need to use the ref keyword when passing reference types. This can be shown in a simple example.

class Program
{
  static void Main(string[] args)
  {
    Program prog = new Program();
    SimpleObject ob = new SimpleObject();
 
    Console.WriteLine("The value of X is " + ob.X.ToString());
    prog.PassRef(ob);
    Console.WriteLine("The value of X is " + ob.X.ToString());
  }
 
  public void PassRef(SimpleObject ob)
  {
    ob.X = 7;
  }
}
 
public class SimpleObject
{
  public SimpleObject()
  {
  }
 
  public int X = 2;
}

The resultant output is:

The value of X is 2
The value of X is 7


Passing parameters using ref

When you pass a reference using the ref keyword, the method expects a pointer to the pointer of an object on the heap. If you don't use the ref keyword, the method expects a pointer of an object on the heap.

Passing parameters using out

Using the out keyword works in the same manner as using the ref keyword, but the argument passed to an out parameter does not have to be initialized first.


Using the reference types


A comparison

For a comparison of value and reference types see Value vs Reference.


MSDN references


Previous_Page_.gif Next_Page_.gif

Today's Deals: Electronics

Personal tools