C#
  Home arrow C# arrow C# Methods, Part 2
Iron Speed
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#

C# Methods, Part 2
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 24
    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

    At the virtual BlackBerry Technical Seminar 2008, you can ask your development questions directly of Research In Motion® (RIM) experts, and take advantage of learning opportunities designed uniquely for BlackBerry solution developers. Register Today!

    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

    - 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

    BlackBerry VTS




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