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.

Passing Reference Types by Reference

Reference type data can be passed to reference parameters. In this case, rather than passing a copy of the reference to the method, as when using value parameters, the reference variable itself is passed. In this situation, property changes within the method are visible outside of the method as expected. However, if the variable is reassigned within the method, the variable outside of the method is also reassigned.

We can demonstrate by modifying the earlier example relating to the Ball class. In the following code the ball is inflated as is was previously with the reference parameters showing no apparent difference in functionality. When the GetNewBall method is called, the ball variable is reassigned as a completely new object. This is mirrored in the Main method of the program also.

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

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

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

        /* OUTPUT

        Diameter: 16
        Diameter: 1

        */
    }

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

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


class Ball
{
    private int _diameter;

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

Output Parameters

There are some occasions where it is useful to return more than one value from a method. Where this is the case, one value can be returned using the normal return command and additional values can be extracted using output parameters.

An output parameter is declared using the out keyword before the parameter type and name. When called, the out keyword is used again as a prefix to the variable being passed to a parameter. The parameter variable must be assigned a value within the method. This value is then returned in the output parameter.

The following code includes a method that uses a return value to return the area of a rectangle. It returns the perimeter of the rectangle using an output parameter. Note that, unlike with a reference parameter, the variable does not need to be assigned before use.

static void Main(string[] args)
{
    int area;
    int perimeter;

    area = CalculateRectangle(5, 10, out perimeter);

    Console.WriteLine("Area: {0}", area);
    Console.WriteLine("Perimeter: {0}", perimeter);
}


static int CalculateRectangle(int width, int height, out int perimeter)
{
    perimeter = (width * 2) + (height * 2);
    return width * height;
}

/* OUTPUT

Area: 50
Perimeter: 30

*/

NB: This code is provided to show an example of output parameters. It would be unusual to create such a method in a real program as it would be more readable to create separate methods for the area and perimeter calculations.

Parameter Arrays

The final type of method argument is the parameter array. This must be the last parameter in a method definition. A parameter array declaration indicates that any number of parameters of the indicated type may be used in the method call, allowing for optional parameters. Each value passed to the parameter is combined into an array that can be processed within the method.

To declare a parameter array, prefix the final parameter with the params keyword and append a pair of square brackets to the data type to specify that it is an array. The following example declares a method with a single string parameter and an integer parameter array. Any number of integer values, include none at all, may be used when calling the method.

static void Main(string[] args)
{
    showScores("Bob", 15);
    showScores("Jill", 10, 12, 15, 25);
    showScores("Ken");
}


static void showScores(string player, params int[] scores)
{
    Console.WriteLine("Player: {0}", player);

    foreach (int score in scores)
    {
        Console.Write("{0}\t", score);
    }

    Console.WriteLine("\n");
}

/* OUTPUT

Player: Bob
15

Player: Jill
10      12      15      25

Player: Ken

*/
5 August 2007