C#
  Home arrow C# arrow 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


    (Page 1 of 5 )

    In this article we will continue our discussion of C# methods, especially passing parameters by value and by reference, and we will also discuss the out and ref keywords. We will end the article with a discussion covering how to create variable parameters methods. An understanding of .NET Types is essential to understanding important parts of this tutorial, so you should read the previous articles (of the series) in order to get the most out of this work.

    When you call a method, your code must provide values for the method's parameters. By default, the method creates its copy of the arguments, so you will have two separate copies (the argument and the parameter). This process is called pass by-value, and it's the default in C#.The behavior will differ depending on the passed type, so if you are passing a Value-Type (in this case the value itself will be copied to the method) then the changes made in the parameter value (the one inside the method) will not affect the argument value, because it's a separate copy.

    If the passed type is a Reference-Type (in this case the reference itself will be copied, not the object's properties and methods, because it's a copy by value), then you will have two references to the same object, and you can change a property value of the object because you have a reference to it.

    There is another parameter vs. argument copying behavior called copy by reference. Again, it differs from Value-Types to Reference-Types, and you have to use the parameter modifier keywords out or ref, as we will discuss soon. For now, when you pass a value type by reference, a reference to the value-type will be passed, and changes made inside the method will be reflected to the argument value. When you pass a Reference-Type, it will pass a reference to the Reference that refers to the object; in other words, it's a double reference.

    So we have four cases here: passing Value-Types by value, passing Value-Types by reference, passing Reference-Types by value and passing Reference-Types by reference. Reading this article will help you to establish a good foundation in C# programming techniques.

    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 4 Hosted by Hostway
    Stay green...Green IT