An array can also be send  to method as argument or parameter. A method process the array and returns output. Sending array as parameter in C# is very easy as passing other value as parameter. Just create a function that take array as argument and then process them. The following example will help you to understand how to send array as argument in C# programming.

Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Array_Parameter_Example
{
    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);
        }
    }
}
In the above example, we create an array and accept some integer value from the user at runtime. Then we send array as argument to PrintArray(int[] newarray)  for printing and other calculation. It is same as other value send as parameter to the function.

Leave a Reply

Your email address will not be published. Required fields are marked *