An array can also be passed to method as argument or parameter. A
method process the array and returns output. Passing array as parameter
in C# is pretty easy as passing other value as parameter. Just create a
function that accepts array as argument and then process them. The
following demonstration will help you to understand how to pass array as
argument in C# programming.
Programming Example of passing array as parameter in C#
Output
Enter number: 5
Enter number: 3
Enter number: 2
Enter number: 1
You entered: 5 3 2 1
Add sum of all value is: 11
Programming Example of passing array as parameter in C#
using System;
namespace array_parameter
{
class Program
{
static void printarray(int[] newarray)
{
int i,sum=0;
Console.Write("\n\nYou entered:\t");
for (i = 0; i < 4; i++)
{
Console.Write("{0}\t", newarray[i]);
sum = sum + newarray[i];
}
Console.Write("\n\nAnd sum of all value is: \t{0}", sum);
Console.ReadLine();
}
static void Main(string[] args)
{
int[] arr = new int[4];
int i;
for (i = 0; i < 4; i++)
{
Console.Write("Enter number:\t");
arr[i] = Convert.ToInt32(Console.ReadLine());
}
// passing array as argument
Program.printarray(arr);
}
}
}
namespace array_parameter
{
class Program
{
static void printarray(int[] newarray)
{
int i,sum=0;
Console.Write("\n\nYou entered:\t");
for (i = 0; i < 4; i++)
{
Console.Write("{0}\t", newarray[i]);
sum = sum + newarray[i];
}
Console.Write("\n\nAnd sum of all value is: \t{0}", sum);
Console.ReadLine();
}
static void Main(string[] args)
{
int[] arr = new int[4];
int i;
for (i = 0; i < 4; i++)
{
Console.Write("Enter number:\t");
arr[i] = Convert.ToInt32(Console.ReadLine());
}
// passing array as argument
Program.printarray(arr);
}
}
}
Output
Enter number: 5
Enter number: 3
Enter number: 2
Enter number: 1
You entered: 5 3 2 1
Add sum of all value is: 11
- In the preceding example, we create an array and accept some integer value from the user at runtime. Then we passed array as argument to printarray(int[] newarray) for printing and other calculation. It is same as other value passed as parameter to the function.
Comments
Post a Comment