C#
  Home arrow C# arrow Page 3 - Value Types and Reference Types
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
VeriSign Whitepapers 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Value Types and Reference Types
By: Ayad Boudiab
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    2008-04-30

    Table of Contents:
  • Value Types and Reference Types
  • Passing Value Types
  • Reference Types
  • Structures

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
    IBM developerWorks
     
    ADVERTISEMENT

    Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    Value Types and Reference Types - Reference Types


    (Page 3 of 4 )


    Reference Types: A reference type, on the other hand, does not store the data itself. Instead, reference types store the address of the data. The address is also referred to as a pointer. The actual data the address refers to is stored in an area of memory called the heap. Here is an illustration:

    A reference type is a class, an interface, an array type, or a delegate type. Like declaring any variable, to declare a variable of a reference type, you start with the type, followed by the variable name, such as:


    Employee e;


    This declaration is not sufficient, however. So fareis referencing nothing. In C# terminology, we say thatecurrently contains the valuenull. This means that we cannot access anyEmployeemember (properties, methods, etc.). To create a meaningfulEmployeeobject with which we can interact, we need thenewoperator and anEmployeeconstructor:

    • Thenewoperator allocates a block of memory for the object. Unlike C++, it is not the responsibility of the programmer to return this block of memory to the heap. The CLR’s garbage collector checks for objects no longer referenced and cleans them up (frees the memory).

    • TheEmployeeconstructor is a special method that has the same name as the class. Its purpose is to initialize the class members.

    Armed with these two new facts, we can now create anEmployeeobject:


    Employee e = new Employee();


    Here is a graphical interpretation of the preceding statement:

    Now, we can access the Employee object. We use the dot (.) operator to do so:


    e.Name; //property

    e.GiveRaise(0.05); //method


    As stated earlier, the variable e contains the address of the Employee object (not the object itself). If we declare anotherEmployeeobject and assign it the value ofe, we will end up with another reference to the sameEmployeeobject thateis currently referencing (we will NOT have anotherEmployeeobject). After all, we are assigning references (or memory addresses).


    Employee e2 = e;


    The graphical representation will be as such:

    So, any changes that we make to the Employee object through one reference (saye2) will be seen by the other reference (e). Note: if you are not interested in this behavior, and you want two separate Employee objects, then you should implement an interface calledClonable. This, however, is outside the scope of this article.

    To see the use of classes in action, let’s take the structure example we used at the beginning of this article and replace the structure with a class. Here is how the code will look:


    class Program

    {

    static voidMain(string[] args)

    {

    Pointp = new Point();

    p.x = 2;

    p.y = 7;

    Console.WriteLine("p-> "+ p);

    Pointp2 = p;

    Console.WriteLine("p2-> "+ p2);

    Console.WriteLine("nAfter changing the Point through p2:n");

    //change the point through p2

    p2.x = 12;

    Console.WriteLine("p2-> "+ p2);

    Console.WriteLine("p-> "+ p);

    }

    }


    class Point

    {

    public intx;

    public inty;

    public override stringToString()

    {

    return string.Format("[{0},{1}]", x, y);

    }

    }


    Output:


     p-> [2,7]

    p2-> [2,7]


    After changing the Point through p2:


    P2-> [12,7]

    p-> [12,7]


    Notice that sincepandp2are referencing the same object, when we changed the value ofxusingp2, the change is also reflected thoughp.

    More C# Articles
    More By Ayad Boudiab


     

    C# ARTICLES

    - Exceptions in C#
    - Overriding versus Overloading
    - Value Types and Reference Types
    - Defining Member and Type Visibility
    - Managing Files in C#
    - Working with Windows Registry in C#
    - Lossless Image Resizing in C#
    - Lossless Image Converting in C#
    - Creating an RSS Feed with ASP.Net Written in...
    - Polymorphism in C#
    - Inheritance in C#
    - C# Events Explained
    - C# Delegates Explained
    - C# StreamReader and StreamWriter Explained
    - C# FileStream Explained

    Iron Speed




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway