CODEDIGEST
Home » Articles
Search
 

Technologies
 

Sponsored links
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Parameters in C# – Part 7

By Selva kumar
Posted On Aug 17,2008
Article Rating:
Be first to rate
this article.
No of Comments: 0
Category: C#
Print this article.

Parameters in C# – Part 7

Introduction

Parameters in C#, Part - 7 of this article series will help us understand, passing the parameters in C#. This part will concentrate on Parameter Arrays with examples wherever necessary.

 

Parameter arrays

Parameter arrays allow a variable number of arguments to be passed into a function member. The definition of the parameter has to include the params modifier, but the use of the parameter has no such keyword. A parameter array has to come at the end of the list of parameters, and must be a single-dimensional array. When using the function member, any number of parameters (including none) may appear in the invocation, so long as the parameters are each compatible with the type of the parameter array. Alternatively, a single array may be passed, in which case the parameter acts just as a normal value parameter.

For example:

void ShowNumbers (params int[] numbers)
{
    foreach (int x in numbers)
    {
        Console.Write (x+" ");
    }
    Console.WriteLine();

}

...

int[] x = {1, 2, 3};
ShowNumbers (x);
ShowNumbers (4, 5);

 

EXAMPLE

using System;
using System.Text;

public class Example9
{
    // Note that ShowNumbers is declared static here just
    // to make the sample app simple, so we don't
    // need to instantiate the example class. This
    // has no bearing on the parameter passing
    // discussed
    static void ShowNumbers (params int[] numbers)
    {
        foreach (int x in numbers)
        {
            Console.Write (x+" ");
        }
        Console.WriteLine();
    }

    public static void Main (string[] args)
    {
        int[] x = {1, 2, 3};
        ShowNumbers (x);
        ShowNumbers (4, 5);
    }

}

 Output:
1 2 3
4 5




In the first invocation, the variable x is passed by value, as it's just an array. In the second invocation, a new array of ints is created containing the two values specified, and a reference to this array is passed.

 

Mini-glossary

Some informal definitions and summaries of terms:

Function member :
         A function member is a method, property, event, indexer, user-defined operator, instance constructor, static constructor, or
destructor.

Output parameter
        A parameter very similar to a reference parameter, but with different definite assignment rules.

Reference parameter (pass-by-reference semantics)
       A parameter which shares the storage location of the variable used in the function member invocation. As they share the same storage location, they always have the same value (so changing the parameter value changes the invocation variable value).

Reference type
       Type where the value of a variable/expression of that type is a reference to an object rather than the object itself.

Storage location
        A portion of memory holding the value of a variable.

Value parameter (the default semantics, which are pass-by-value)
        A value parameter that has its own storage location, and thus its own value. The initial value is the value of the expression used in the function member invocation.

Value type
        Type where the value of a variable/expression of that type is the object data itself.

Variable
        Name associated with a storage location and type. (Usually a single variable is associated with a storage location. The exceptions are for reference and output parameters.)

Conclusion

With this article we will end of this article series, Parameter in C#. This article series will help us understand the parameters in C# in detail and with examples wherever necessary. Don’t hesitate to post your queries in the feedback section of this article.

Thanks for reading this article!

Similar Articles
You can contribute to CodeDiget.Com:
Donate to CodeDigest.com
Article Feedback
Comments