C#
  Home arrow C# arrow Page 4 - 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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Moblin 
JMSL Numerical Library 
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 / 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 - The out keyword


    (Page 4 of 5 )

    The out parameter modifier keyword has the same effects as the ref keyword, with one important difference. With the ref keyword, you must initialize the variable before you pass it as the method parameters, but with the out keyword you can initialize the variable inside the method as follows:

    using System;
    namespace MyCompany
    {
      public class MethodTest
      {

        static void Main()
        {
          int ValueType;
          TakeNumber(out ValueType);
          Console.WriteLine("After calling the method the value of" + "the variable ValueType = {0}", ValueType);
          Console.ReadLine();
        }

        static void TakeNumber(out int x)
        {
          x = 10;
          Console.WriteLine("Inside the method we initialized x, x = {0}", x);
          x++;
          Console.WriteLine("we increased x by one inside the method, x = {0}", x);
        }
      }
    }

    The result will be:

    We have passed an uninitialized variable to the method, and then we have initialized the variable inside the method. Note that the effects of the out keyword are the same as the ref keyword, so it's considered pass by reference behavior.

    More C# Articles
    More By Michael Youssef


     

    C# ARTICLES

    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...
    - 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...





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