C#
  Home arrow C# arrow Page 3 - C# Methods, Part 2
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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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#

C# Methods, Part 2
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 25
    2005-05-03

    Table of Contents:
  • C# Methods, Part 2
  • Handling Value-Type Parameters
  • Handling Reference-Type Parameters
  • The out keyword
  • Variable Length Parameters

  • 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


    C# Methods, Part 2 - Handling Reference-Type Parameters


    (Page 3 of 5 )

    When you pass an object variable to a method parameter, the default behavior is to copy by value -- but in the case of a Reference-Type, the copied value is not the object itself. In other words, the copied value is not the object fields and methods. What actually happens is that a reference to the object is just copied from the object variable to the method parameter, and this is similar for to having a Value-Type passed by reference (using the ref keyword).

    When you pass a Value-Type by reference, a reference to the variable is passed to the method. This means that both the variable (which contains the value itself) and the method parameter (which contains a reference to the value) refer to the same data. Passing a Reference-Type by value differs. When you pass a Reference-Type by value, a copy of the object reference is passed to the method parameter, which means that we have one object located on the Heap, with two references located on the Stack. Actually, it is worth noting that the object reference is a value that lives on the Stack, and it refers to the actual object on the Heap.

    So let's look at an example:

    using System;
    namespace MyCompany
    {
      public class MethodTest
      {

        static void Main()
        {
          Order order = new Order();
          order.OrderNumber = 12345;
          Console.WriteLine("order.OrderNumber = {0}",order.OrderNumber);
          TakeObject(order); 
          Console.WriteLine("after the method call " + "order.OrderNumber = {0}",order.OrderNumber);
          Console.ReadLine(); 
        }

        static void TakeObject(Order ord)
        {
          ord.OrderNumber = 54321;
          Console.WriteLine("ord.OrderNumber = {0}", ord.OrderNumber);
        }
      }

      public class Order
      { 
        public int OrderNumber; 
      } 
    }

    The result will be:

    As you can see, the Main method creates an object of type Order and assigns the value 12345 to the OrderNumber field of that object. Then it prints to the Console the value of the object's field, calls the static method TakeObject, and passes the order object to it. The method assigns a new value to the parameter and terminates. The Main method again prints the value of the object's field and, as you can see, it has been changed after the method call. To make it clear that the method just has another reference to the object, take a look at the following revised version of the above code:

    using System;
    namespace MyCompany
    {
      public class MethodTest
      {

        static void Main()
        {
          Order order = new Order();
          order.OrderNumber = 12345;
          Console.WriteLine("order.OrderNumber = {0}",order.OrderNumber);
          TakeObject(order);
          Console.WriteLine("after the method call " + "order.OrderNumber = {0}",order.OrderNumber);
          Console.ReadLine();
        }

        static void TakeObject(Order ord)
        {
          ord = null;
        } 
      }  
     
      public class Order
      { 
        public int OrderNumber;
      }
    }

    Now the result is:

    We just assign null to the method parameter to prove that it's just another reference. When we assigned the reference to null, the object had not been affected, and the order .OrderNumber still equals to the same value (12345).

    You can also pass a Reference-Type by reference, but it's dangerous and should be avoided except for the situations that you are sure you need it. For example, you can pass an object to a method (by reference using the ref keyword) and the method can destroy your object, or even assign it to a new, different one. The next example is a modified version of the above code:

    using System;
    namespace MyCompany
    {
      public class MethodTest
      { 

        static void Main()
        {
          Order order = new Order();
          order.OrderNumber = 12345;
          Console.WriteLine("order.OrderNumber = {0}",order.OrderNumber);
          TakeObject(ref order);
          Console.WriteLine("after the method call " + "order.OrderNumber = {0}",order.OrderNumber);
          Console.ReadLine();
        }

        static void TakeObject(ref Order ord)
        {
          Order newOrder = new Order();
          newOrder.OrderNumber = 987123;
          ord = newOrder;
          Console.WriteLine("The method assign a new object to the parameter" + "and passing 987123 to OrderNumber");
        }
      }

      public class Order
      {
        public int OrderNumber;
      }
    }

    The result is:

    The method TakeObject takes an Order object parameter and destroys it by creating a new one and assigning a new value to the field OrderNumber. So passing a Reference-Type by reference makes a double reference. In other words, it makes a reference to the reference, which refers to the object. In our example, when we pass the order object reference to the method, the ref keyword creates a reference to the order object reference, and when the method makes any changes it will be reflected to the reference (because we have a reference to the object reference). In our example, we assigned a new object to the parameter ord, which in turn assigned this new object reference to the calling code object reference.

    More C# Articles
    More By Michael Youssef


     

    C# ARTICLES

    - Cyclic Redundancy Check
    - Handling Methods and Functions
    - Destroying Objects in C#
    - Creating Objects in C-Sharp
    - Classes and Objects
    - Programming Languages: Managed versus Native
    - LINQ-to-MySQL with DbLinq in C#
    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT