Monday, October 4, 2010

Understanding the Parameter array in C #.

Parameter array: -
A parameter array enables a many –to- many relationship: many arguments can be represented by single parameter array. In other words, parameter array enable variable length argument lists.
A parameter array is declared with a params modifier. There can be only one parameter array for given method, and it must be the right-most parameter. The type of a parameter array is always a single dimensional array type. A caller can either pass a single argument of this array type, or any number of arguments of the element type of this array type.

Example:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace param_parameter
{

class Test
{

class Test1
{

public void print(params int[] numbers)
{

foreach (int x in numbers)
{

Console.WriteLine(" " + x);

}

}

}

static void Main(string[] args)
{

int[] numbers = { 10, 20, 30, 40, 50, 60 };

int a = 100, b = 200, c = 300, d = 400;

Test1 obj = new Test1();

obj.print(a, b, c, d);

obj.print(numbers);

Console.ReadLine();

}

}

}

The output is display below.

100
200
300
400
10
20
30
40
50
60





What Other's are reading.
Understanding the passing value Parameter in C #.
Understanding the Reference Parameter in C #.
Understanding the output Parameter in C #.

No comments:

Recent Posts