Team LiB
Previous Section Next Section

Passing by Value and by Reference

Arguments can be passed in two fashions: by value or reference. If you pass an argument by value, you are essentially creating a copy of the existing variable to the method being called.

Caution 

It is extremely important to know that any changes made to the variable will not be returned to the calling function when you pass a variable by value!

Arguments can also be passed by reference. This requires a couple of steps:

By passing arguments by reference, you are allowing the called function to make modifications to the object being passed, and those modifications will be present when the called function returns.

Table 5-3 describes the keywords used for passing arguments.

Table 5-3: Keywords for Passing Arguments

Keyword

Description

in

Passes a parameter by value to the called method. Modifications made to the object inside the called method will not be reflected after returning.

out

Used to specify that the called method will create the object being returned.

ref

Passes a reference of an object that can be modified to the called method.

params

Allows you to pass an unknown number of parameters of a single type. This can be used by only one parameter in a method. You can pass a single argument of a type, an array, or a variable number of arguments.

Note 

Methods in C# have the ability to receive a value back for error checking or modification if you desire. It is important to note that conducting error checking with return values is not the preferred method in C#. It is recommended to throw exceptions instead. We will look at exceptions in a Chapter 8.

The following example demonstrates passing arguments by value and by reference.

Code Example: Passing by Value and by Reference
Start example

using System;

namespace Client.Chapter_5___Building_Your_Own_Classes
{
      class PassingParametersByValueandByRef
      {
            static private int MyInt = 5;
            static public int MyInt2 = 10;
            static public int[] MyIntArray;
            private static int ObjectCount = 0;
            static void Main(string[] args)
            {
                  int MyInt = 5;
                  MyIntArray = new int[10];
                  ObjectCount++;
                  Method2();
                  Method2(1, 2);
                  MyMethodRef(ref MyInt);
                  Method2(new int[] { 1, 2, 3, 4 });
            }
            //Pass by reference
            static public int MyMethodRef(ref int myInt)
            {
                  MyInt = MyInt + myInt;
                  return MyInt;
            }
            //Pass by value
            static public int MyMethod(int myInt)
            {
                  MyInt = MyInt + myInt;
                  return MyInt;
            }
            static private long MyLongMethod(ref int myInt)
            {
                  return myInt;
            }
            static public void Method2(params int[] args)
            {
                  for (int I = 0; I < args.Length; I++)
                        Console.WriteLine(args[I]);
            }
      }
}
End example

Team LiB
Previous Section Next Section