C#
  Home arrow C# arrow Page 2 - 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 
Moblin 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
IBM developerWorks
 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
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Value Types and Reference Types - Passing Value Types


    (Page 2 of 4 )


    When it comes to passing value types between methods, it is important to note that only a copy of the value is passed into the method. Any manipulations that you do to the copy will not affect the original value. Here is an illustration:


    class Program

    {

    static voidMain(string[] args)

    {

    Pointp = new Point();

    p.x = 2;

    p.y = 7;

    Console.WriteLine("Before an attempt to move the point: {0}", p);

    Move(p, 2, 2);

    Console.WriteLine("After the attempt to move the point: {0}", p);

    }


    static voidMove(Pointp, intx_value, inty_value)

    {

    p.x += x_value;

    p.y += y_value;

    }

    }


    struct Point

    {

    public intx;

    public inty;

    public override stringToString()

    {

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

    }

    }


    The output of the program will be:


    Before an attempt to move the point: [2,7]

    After the attempt to move the point: [2,7]


    The first question that comes to mind is: what happened to the changes I made to the structure?

    Answer: p is a Point, which is a structure, which is a value type. Passing a value type to a method results in a copy being made to the value type (p in this case). The copy is passed into the method and changes are made to the copy (the copy of p will have the values [4,9], the same value you anticipated for p). By the time you hit the closing bracket for the Move function, the copy of p will be out of scope. So, you never changed p, you only changed a copy of p.



    Considering that parameters are passed via a value by default, the question is: is there any way to change that behavior? Yes, that is done using therefmodifier. If you place therefmodifier in front of a value parameter, the parameter is then passed via reference instead, and obviously any changes that you make to the parameter will be maintained after the method exits. Keep in mind, though, therefmodifier needs to be placed in the method declaration and the method call. If we return to the structure example, here is how it will look:


    class Program

    {

    static voidMain(string[] args)

    {

    Pointp;

    p.x = 2;

    p.y = 7;

    Console.WriteLine("Before an attempt to move the point: {0}", p);

    Move(refp, 2, 2);

    Console.WriteLine("After the attempt to move the point: {0}", p);

    }


    static voidMove(ref Pointp, intx_value, inty_value)

    {

    p.x += x_value;

    p.y += y_value;

    }

    }


    struct Point

    {

    public intx;

    public inty;

    public override stringToString()

    {

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

    }

    }


    Notice how the refmodifier is used in the Move() method (declaration and call).

    Now the output of the program is changed to:


    Before an attempt to move the point: [2,7]

    After the attempt to move the point: [4,9]

    More C# Articles
    More By Ayad Boudiab


     

    C# ARTICLES

    - Color Transformation in C# GDI+ Programming
    - 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





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