BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

C# Programming
.NET 1.1+

C# Method Parameters

The forty-sixth part of the C# Fundamentals tutorial expands upon the creation of methods described in the previous instalments. In this article, the various types of parameter that can be added to a method definition are described.

Value Parameters

Passing Value Types by Value

So far in the C# Fundamentals tutorial, we have created methods that accept parameters and return a single value. These parameters have all been passed by value. In the case of passing value types, this means that a copy of the value's contents has been passed. As only a copy of the contents is available, the external variable's value cannot be modified from within the method.

The following example code demonstrates the use of parameters with variables that are value types. In this sample, a value type variable is declared and its value is assigned. This variable is then passed to a parameter by value and the parameter's value is changed within the method. The original variable remains unaffected.

static void Main(string[] args)
{
    int original = 100;		
    showDouble(original);
    Console.WriteLine("Original: {0}", original);
}

static void showDouble(int valueToShow)
{
    valueToShow *=2;
    Console.WriteLine("Double: {0}", valueToShow);
}

/* OUTPUT

Double: 200
Original: 100

*/

Passing Reference Types by Value

In the case of passing reference types by value, a copy of the reference is made. This means that any changes to the object's properties that occur within the method will be visible outside of the method too. This is because both the external variable and the variable within the method contain matching references to the same underlying object data. However, if the variable inside the method is assigned a completely new value, and therefore a different reference, this change is not visible outside of the method.

To demonstrate, consider the following code. This uses a "Ball" class that contains a single property for the ball's diameter. When calling the InflateBall method, the Ball is passed by reference. Increasing the diameter property within the method is visible outside of the method too. However, when the GetNewBall method is called, the reassigned object is not seen externally to the method.

You can test this code by creating a new console application and copying in the Program and Ball class definitions. NB: This code is for demonstration purposes only and should not be considered well structured.

class Program
{
    static void Main(string[] args)
    {
        Ball football = new Ball();
        football.Diameter = 15;

        InflateBall(football);
        Console.WriteLine("Diameter: {0}", football.Diameter);

        GetNewBall(football);
        Console.WriteLine("Diameter: {0}", football.Diameter);

        /* OUTPUT

        Diameter: 16
        Diameter: 16

        */
    }

    static void InflateBall(Ball ball)
    {
        ball.Diameter++;
    }

    static void GetNewBall(Ball ball)
    {
        ball = new Ball();
        ball.Diameter = 1;
    }
}


class Ball
{
    private int _diameter;
    
    public int Diameter
    {
        get
        {
            return _diameter;
        }
        set
        {
            _diameter = value;
        }
    }
}

NB: If a value parameter uses a reference type, changes to the properties of the object are reflected outside of the method in most cases. An example where this is not true would be for the immutable string class, which is designed to act like a value type in some of its behaviour.

Reference Parameters

Passing Value Types by Reference

An alternative to passing parameters by value is passing by reference. When using reference parameters for value types a copy of the value is not made. Instead, the variable used in the parameter is itself passed to the called method. This makes the behaviour similar to using reference types in value parameters; any changes to the value of the parameter variable is also seen outside of the method.

A reference parameter is declared using the ref keyword before the parameter type. This prefix must also be used when calling the method.

The following sample code is based upon the first example in this article but by using a reference parameter rather than a value parameter, the results seen are different.

static void Main(string[] args)
{
    int original = 100;		
    showDouble(ref original);
    Console.WriteLine("Original: {0}", original);
}

static void showDouble(ref int valueToShow)
{
    valueToShow *=2;
    Console.WriteLine("Double: {0}", valueToShow);
}

/* OUTPUT

Double: 200
Original: 200

*/
5 August 2007